phraseapp_android 0.1.0 → 0.2.0

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: f1bfba10c4b6cbe4cdcebe6b43238b0f5831aee6
4
- data.tar.gz: 04b26134b3a91b71167c0faca54154e1e023caaa
3
+ metadata.gz: e93e77c6176e4fae785005ad6e07522cf258961b
4
+ data.tar.gz: 1801f2e7bda8d371c5240145b69ee0671aa022fd
5
5
  SHA512:
6
- metadata.gz: 57e1cf5c6bebf15704a1814ead25ee5892a07a9d5d95dfe8487c804aebda8f9925ba80b2c9588676aabd849197bb01c7d8d7768f338d94112c128e4929117f3d
7
- data.tar.gz: ccc4b1ea8f4eff82714bd367e4dccdaa38ab976e646e41bf4b7885f28af456e34f022d53a28d18c5d8701c6ef6c023d984bf1ecf86b99fa227ef29986c51a0af
6
+ metadata.gz: 4436a3b1498b843cc05d71787e909716327ec6c34b5a652c8b430e354f62b9c2800337937bb194f9045c4117e1c4f4353c521eed73f211f6d4a67f9bc4e157c3
7
+ data.tar.gz: 740ddce5b12a579e27ee4aae46fde0aba2e02155fe0651ed0e856728481fd926abfe1e8349c55e7842d15b39aa3e53a38a87ed42a19cd0311b9bccc140940a77
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /pkg/
9
9
  /spec/reports/
10
10
  /tmp/
11
+ *.gem
@@ -39,50 +39,44 @@ class PhraseApp::Android::MissingTranslations < PhraseApp::Android::PhraseAppCli
39
39
  end
40
40
 
41
41
  def pull_locale(locale)
42
- strings_updated = 0
43
- arrays_updated = 0
44
- missing = find locale
45
- if missing.size > 0
46
- strings = read_locale_file 'strings', locale
47
- arrays = read_locale_file 'arrays', locale
48
-
49
- params = PhraseApp::RequestParams::LocaleDownloadParams.new file_format: 'xml'
50
- doc = Nokogiri::XML client.locale_download(project_id, locale, params)
51
-
52
- missing.each do |el|
53
- name = el.attr 'name'
54
- translated = doc.at('//resources').search("#{el.name}[@name=#{name}]").first
55
- if translated
56
- if el.name == 'string'
57
- str = Nokogiri::XML::Node.new 'string', strings
58
- str['name'] = name
59
- str.content = translated.text
60
- strings.at('//resources').children.last.after str
61
- strings_updated += 1
62
- elsif el.name == 'string-array'
63
- str = Nokogiri::XML::Node.new 'string-array', arrays
64
- str['name'] = name
65
- translated.element_children.each do |child|
66
- item = Nokogiri::XML::Node.new 'item', arrays
67
- item.content = child.text
68
- str.add_child item
69
- end
70
- arrays.at('//resources').children.last.after str
71
- arrays_updated += 1
72
- end
73
- end
74
- end
42
+ stats = {
43
+ strings: {added: 0, updated: 0},
44
+ arrays: {added: 0, updated: 0}
45
+ }
75
46
 
76
- if strings_updated + arrays_updated > 0
77
- PhraseApp::Android::FileFormatter.new.apply('strings', locale) if strings_updated > 0
78
- PhraseApp::Android::FileFormatter.new.apply('arrays', locale) if arrays_updated > 0
79
- puts "#{strings_updated + arrays_updated} translation keys were updated.".green
80
- else
81
- puts 'no keys were updated.'.yellow
82
- end
47
+ strings = doc_to_hash read_locale_file('strings', locale)
48
+ arrays = doc_to_hash read_locale_file('arrays', locale)
49
+ current = strings.merge arrays
50
+
51
+ params = PhraseApp::RequestParams::LocaleDownloadParams.new file_format: 'xml'
52
+ doc = Nokogiri::XML client.locale_download(project_id, locale, params)
53
+ recent = doc_to_hash doc
54
+
55
+ merge_translations current, recent, :strings, stats
56
+ merge_translations current, recent, :arrays, stats
57
+
58
+ formatter = PhraseApp::Android::FileFormatter.new
59
+
60
+ if stats[:strings][:added] + stats[:strings][:updated] > 0
61
+ updated_strings = formatter.apply_to_xml_doc hash_to_doc(current[:strings])
62
+ write_to_file locale_file_name('strings', locale), updated_strings
83
63
  end
84
64
 
85
- [strings_updated, arrays_updated]
65
+ if stats[:arrays][:added] + stats[:arrays][:updated] > 0
66
+ updated_arrays = formatter.apply_to_xml_doc hash_to_doc(current[:arrays])
67
+ write_to_file locale_file_name('arrays', locale), updated_arrays
68
+ end
69
+
70
+ added = stats[:strings][:added] + stats[:arrays][:added]
71
+ updated = stats[:strings][:updated] + stats[:arrays][:updated]
72
+
73
+ if added + updated > 0
74
+ puts "#{added + updated} keys were updated!".green
75
+ else
76
+ puts 'no keys were updated.'.yellow
77
+ end
78
+
79
+ stats
86
80
  end
87
81
 
88
82
  private
@@ -104,4 +98,58 @@ class PhraseApp::Android::MissingTranslations < PhraseApp::Android::PhraseAppCli
104
98
  [strings, arrays]
105
99
  end
106
100
 
101
+ def doc_to_hash(doc)
102
+ result = {strings: {}, arrays: {}}
103
+ doc.at('//resources').element_children.each do |el|
104
+ if el.name == 'string'
105
+ result[:strings][el.attr('name')] = el.text
106
+ elsif el.name == 'string-array'
107
+ result[:arrays][el.attr('name')] = el.element_children.map { |c| c.text }
108
+ end
109
+ end
110
+ result.reject { |k, v| v.empty? }
111
+ end
112
+
113
+ def merge_translations(current, updated, key, stats)
114
+ updated[key].each do |name, values|
115
+ current[key] ||= {}
116
+ current_value = current[key][name]
117
+ if current_value
118
+ if values != current_value
119
+ current[key][name] = values
120
+ stats[key][:updated] += 1
121
+ end
122
+ else
123
+ current[key][name] = values
124
+ stats[key][:added] += 1
125
+ end
126
+ end
127
+ end
128
+
129
+ def hash_to_doc(source)
130
+ doc = Nokogiri::XML::Document.new
131
+ doc.encoding = 'utf-8'
132
+ res = doc.create_element 'resources', 'xmlns:tools' => 'http://schemas.android.com/tools'
133
+ doc.add_child res
134
+
135
+ source.each do |name, value|
136
+ if value.is_a? Array
137
+ str = Nokogiri::XML::Node.new 'string-array', res
138
+ str['name'] = name
139
+ value.each do |text_item|
140
+ item = Nokogiri::XML::Node.new 'item', str
141
+ item.content = text_item
142
+ str.add_child item
143
+ end
144
+ res.add_child str
145
+ else
146
+ str = Nokogiri::XML::Node.new 'string', res
147
+ str['name'] = name
148
+ str.content = value
149
+ res.add_child str
150
+ end
151
+ end
152
+ doc
153
+ end
154
+
107
155
  end
@@ -47,8 +47,12 @@ module PhraseApp
47
47
  end
48
48
 
49
49
  def write_xml_to_file(path, doc)
50
+ write_to_file path, doc.to_xml(ident: 4)
51
+ end
52
+
53
+ def write_to_file(path, contents)
50
54
  File.open path, 'w' do |f|
51
- f.write doc.to_xml(ident: 4)
55
+ f.write contents
52
56
  end
53
57
  end
54
58
 
@@ -1,3 +1,3 @@
1
1
  module PhraseappAndroid
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phraseapp_android
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Glukhov (serggl)