tolq-parsers-xliff 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/xliff/parser.rb +44 -13
- data/lib/xliff/unparser.rb +6 -4
- data/lib/xliff/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31536f271f89a7333c67950065ce2a4347600f0f
|
4
|
+
data.tar.gz: 025b219b7a2232b029f0abcdaa5ec028c9943912
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dee89cc6c7de47ba31d6772def5b7af58e0d96c72ac085eff2aef4e029533a945391aef017f1419061a2bd1747d9af8a02115007f5a1f1272d31b37e739b24b
|
7
|
+
data.tar.gz: 19ecc354b22d575eb5cd3012fe45b5303e47202bb14ee45664923dbaec05f0a200a892b11924bcb35555dad1c40cb5501cdd2bd15bcdb30ff435be46147a96a9
|
data/lib/xliff/parser.rb
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
module Tolq::Parsers::Xliff
|
2
|
+
class ParseError < StandardError; end;
|
3
|
+
|
2
4
|
class Parser
|
5
|
+
# Helper method to convert a xliff into an annotated hash. Format is
|
6
|
+
# different from Tolq api
|
7
|
+
#
|
8
|
+
# @param xliff_text [String] String of file
|
9
|
+
# @param parse_opts [Hash] Noop
|
10
|
+
# @return [Hash] Hash representation of xliff
|
11
|
+
def self.to_hash(xliff_text, **parse_opts)
|
12
|
+
xliff = XLIFFer::XLIFF.new(xliff_text)
|
13
|
+
validate_xliff!(xliff)
|
14
|
+
extract_request_data_from_files(xliff.files, text_node: false)
|
15
|
+
end
|
16
|
+
|
3
17
|
|
4
18
|
# Creates a new Xliff parser
|
5
19
|
#
|
@@ -17,34 +31,51 @@ module Tolq::Parsers::Xliff
|
|
17
31
|
# @return [Hash] A hash suitable to be converted to json for the Tolq Api
|
18
32
|
def parse(xliff_text)
|
19
33
|
xliff = XLIFFer::XLIFF.new(xliff_text)
|
20
|
-
|
21
|
-
request_data =
|
34
|
+
self.class.validate_xliff!(xliff)
|
35
|
+
request_data = self.class.extract_request_data_from_files(xliff.files)
|
22
36
|
|
23
37
|
{
|
24
38
|
"request" => request_data,
|
25
|
-
"source_language_code" => @source_language_code || strip_region_language(
|
26
|
-
"target_language_code" => @target_language_code || strip_region_language(
|
39
|
+
"source_language_code" => @source_language_code || strip_region_language(xliff.files.first.source_language),
|
40
|
+
"target_language_code" => @target_language_code || strip_region_language(xliff.files.first.target_language),
|
27
41
|
"quality" => @quality.to_s
|
28
42
|
}
|
29
43
|
end
|
30
44
|
|
31
45
|
private
|
32
46
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
|
47
|
+
def self.extract_request_data_from_files(xliff_files, text_node: true)
|
48
|
+
acc = {}
|
49
|
+
xliff_files.each.with_index do |file, file_idx|
|
50
|
+
file.strings.each do |string|
|
51
|
+
unless string.source && string.source.strip.length > 0
|
52
|
+
next acc
|
53
|
+
end
|
54
|
+
id = "#{file_idx}-#{string.id}"
|
55
|
+
if text_node
|
56
|
+
acc[id] = {"text" => string.source }
|
57
|
+
else
|
58
|
+
acc[id] = string.source
|
59
|
+
end
|
60
|
+
if string.note.length > 0 && text_node # xliffs so far use this for bogus anyway
|
61
|
+
acc[id].merge!("translator_message" => string.note)
|
62
|
+
end
|
37
63
|
end
|
38
|
-
acc[string.id] = {"text" => string.source }
|
39
|
-
if string.note.length > 0
|
40
|
-
acc[string.id].merge!("translator_message" => string.note)
|
41
|
-
end
|
42
|
-
acc
|
43
64
|
end
|
65
|
+
acc
|
44
66
|
end
|
45
67
|
|
46
68
|
def strip_region_language(language)
|
47
69
|
language[0..1]
|
48
70
|
end
|
71
|
+
|
72
|
+
def self.validate_xliff!(xliff)
|
73
|
+
if xliff.files.map(&:source_language).compact.uniq.length != 1
|
74
|
+
raise ParseError.new("More than one source language, cannot parse file")
|
75
|
+
end
|
76
|
+
if xliff.files.any? { |file| file.strings.map(&:id).any?(&:nil?) }
|
77
|
+
raise ParseError.new("Missing mandatory ID for one or more 'trans-unit'")
|
78
|
+
end
|
79
|
+
end
|
49
80
|
end
|
50
81
|
end
|
data/lib/xliff/unparser.rb
CHANGED
@@ -25,10 +25,12 @@ module Tolq::Parsers::Xliff
|
|
25
25
|
|
26
26
|
def translate_xliff(translations, xliff_text, target_language_code)
|
27
27
|
xliff = XLIFFer::XLIFF.new(xliff_text)
|
28
|
-
xliff.files.
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
xliff.files.each.with_index do |file, file_idx|
|
29
|
+
file.target_language = target_language_code
|
30
|
+
file.strings.each do |string|
|
31
|
+
if translation = translations["#{file_idx}-#{string.id}"]
|
32
|
+
string.target = translation
|
33
|
+
end
|
32
34
|
end
|
33
35
|
end
|
34
36
|
xliff
|
data/lib/xliff/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tolq-parsers-xliff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Timon Vonk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: xliffer
|