phraseapp_android 0.1.0 → 0.2.0
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/.gitignore +1 -0
- data/lib/phraseapp_android/missing_translations.rb +89 -41
- data/lib/phraseapp_android/phrase_app_client.rb +5 -1
- data/lib/phraseapp_android/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e93e77c6176e4fae785005ad6e07522cf258961b
|
4
|
+
data.tar.gz: 1801f2e7bda8d371c5240145b69ee0671aa022fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4436a3b1498b843cc05d71787e909716327ec6c34b5a652c8b430e354f62b9c2800337937bb194f9045c4117e1c4f4353c521eed73f211f6d4a67f9bc4e157c3
|
7
|
+
data.tar.gz: 740ddce5b12a579e27ee4aae46fde0aba2e02155fe0651ed0e856728481fd926abfe1e8349c55e7842d15b39aa3e53a38a87ed42a19cd0311b9bccc140940a77
|
data/.gitignore
CHANGED
@@ -39,50 +39,44 @@ class PhraseApp::Android::MissingTranslations < PhraseApp::Android::PhraseAppCli
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def pull_locale(locale)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
[
|
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
|
55
|
+
f.write contents
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|