tolq-parsers-xliff 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54e8257412582072e447ab41106b4354254a25a8
4
- data.tar.gz: 0a17033cc0e99e8b53a0fae54681c7e1992f4c86
3
+ metadata.gz: 31536f271f89a7333c67950065ce2a4347600f0f
4
+ data.tar.gz: 025b219b7a2232b029f0abcdaa5ec028c9943912
5
5
  SHA512:
6
- metadata.gz: 823ef5c99d158ab0a2cf3e62360e06298c1e5d61369955cdf9f15205f25b247c00970e265f02237702022db41c9873301678b08c2e5de1c927a5ec1a8806b7fe
7
- data.tar.gz: 22df13c0a2a35bbca71e69ec5a359d5527b0ab03ced6cbeccbea35975b832c83a9530e26f54f534cf366ce25c44faa125f05bd1ce75f8b51fff5053e9e3b191b
6
+ metadata.gz: 9dee89cc6c7de47ba31d6772def5b7af58e0d96c72ac085eff2aef4e029533a945391aef017f1419061a2bd1747d9af8a02115007f5a1f1272d31b37e739b24b
7
+ data.tar.gz: 19ecc354b22d575eb5cd3012fe45b5303e47202bb14ee45664923dbaec05f0a200a892b11924bcb35555dad1c40cb5501cdd2bd15bcdb30ff435be46147a96a9
@@ -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
- file = xliff.files.first
21
- request_data = extract_request_data_from_file(file)
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(file.source_language),
26
- "target_language_code" => @target_language_code || strip_region_language(file.target_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 extract_request_data_from_file(xliff_file)
34
- xliff_file.strings.inject({}) do |acc, string|
35
- unless string.source && string.source.strip.length > 0
36
- next acc
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
@@ -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.first.target_language = target_language_code
29
- xliff.files.first.strings.each do |string|
30
- if translation = translations[string.id]
31
- string.target = translation
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
@@ -1,7 +1,7 @@
1
1
  module Tolq
2
2
  module Parsers
3
3
  module Xliff
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.6"
5
5
  end
6
6
  end
7
7
  end
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.5
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-16 00:00:00.000000000 Z
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xliffer