Left: | ||
Right: |
OLD | NEW |
---|---|
1 # This Source Code Form is subject to the terms of the Mozilla Public | 1 # This Source Code Form is subject to the terms of the Mozilla Public |
2 # License, v. 2.0. If a copy of the MPL was not distributed with this | 2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 import errno | 5 import errno |
6 import io | 6 import io |
7 import json | 7 import json |
8 import os | 8 import os |
9 import re | 9 import re |
10 from StringIO import StringIO | 10 from StringIO import StringIO |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 ).encode('utf-8') | 184 ).encode('utf-8') |
185 | 185 |
186 | 186 |
187 def toJson(data): | 187 def toJson(data): |
188 return json.dumps( | 188 return json.dumps( |
189 data, ensure_ascii=False, sort_keys=True, | 189 data, ensure_ascii=False, sort_keys=True, |
190 indent=2, separators=(',', ': ') | 190 indent=2, separators=(',', ': ') |
191 ).encode('utf-8') + '\n' | 191 ).encode('utf-8') + '\n' |
192 | 192 |
193 | 193 |
194 def import_string_webext(data, key, source): | |
195 """Import a single translation from the source dictionary into data""" | |
196 data[key] = source | |
197 | |
198 | |
199 def import_string_gecko(data, key, value): | |
200 """Import Gecko-style locales into data. | |
201 | |
202 Only sets {'message': value} in the data-dictionary, after stripping | |
203 undesired Gecko-style access keys. | |
204 """ | |
205 match = re.search(r'^(.*?)\s*\(&.\)$', value) | |
206 if match: | |
207 value = match.group(1) | |
208 else: | |
209 index = value.find('&') | |
210 if index >= 0: | |
211 value = value[0:index] + value[index + 1:] | |
212 | |
213 data[key] = {'message': value} | |
214 | |
215 | |
216 def import_locales(params, files): | 194 def import_locales(params, files): |
217 for item in params['metadata'].items('import_locales'): | 195 for item in params['metadata'].items('import_locales'): |
218 filename, keys = item | 196 filename, keys = item |
219 for sourceFile in glob.glob(os.path.join(os.path.dirname(item.source), | 197 for sourceFile in glob.glob(os.path.join(os.path.dirname(item.source), |
220 *filename.split('/'))): | 198 *filename.split('/'))): |
221 parts = sourceFile.split(os.path.sep) | 199 locale = sourceFile.split(os.path.sep)[-2].replace('-', '_') |
Sebastian Noack
2017/10/06 19:13:28
As pointed out on the review introducing this code
tlucas
2017/10/09 23:18:07
Done.
| |
222 locale = parts[-2].replace('-', '_') | |
223 targetFile = os.path.join('_locales', locale, 'messages.json') | 200 targetFile = os.path.join('_locales', locale, 'messages.json') |
224 data = json.loads(files.get(targetFile, '{}').decode('utf-8')) | 201 data = json.loads(files.get(targetFile, '{}').decode('utf-8')) |
225 | 202 |
226 try: | 203 try: |
227 # The WebExtensions (.json) and Gecko format provide | 204 with io.open(sourceFile, 'r', encoding='utf-8') as handle: |
228 # translations differently and/or provide additional | 205 sourceData = json.load(handle) |
229 # information like e.g. "placeholders". We want to adhere to | |
230 # that and preserve the addtional info. | |
231 if sourceFile.endswith('.json'): | |
232 with io.open(sourceFile, 'r', encoding='utf-8') as handle: | |
233 sourceData = json.load(handle) | |
234 import_string = import_string_webext | |
235 else: | |
236 import localeTools | |
237 sourceData = localeTools.readFile(sourceFile) | |
238 import_string = import_string_gecko | |
239 | 206 |
240 # Resolve wildcard imports | 207 # Resolve wildcard imports |
241 if keys == '*' or keys == '=*': | 208 if keys == '*' or keys == '=*': |
242 importList = sourceData.keys() | 209 importList = sourceData.keys() |
243 importList = filter(lambda k: not k.startswith('_'), importL ist) | 210 importList = filter(lambda k: not k.startswith('_'), importL ist) |
244 if keys == '=*': | 211 if keys == '=*': |
245 importList = map(lambda k: '=' + k, importList) | 212 importList = map(lambda k: '=' + k, importList) |
246 keys = ' '.join(importList) | 213 keys = ' '.join(importList) |
247 | 214 |
248 for stringID in keys.split(): | 215 for stringID in keys.split(): |
249 noMangling = False | 216 noMangling = False |
250 if stringID.startswith('='): | 217 if stringID.startswith('='): |
251 stringID = stringID[1:] | 218 stringID = stringID[1:] |
252 noMangling = True | |
Sebastian Noack
2017/10/06 19:13:28
Since you are unsupporting the mangle feature, it
tlucas
2017/10/09 23:18:07
Done. I also adjusted the content in #5837 - feel
Sebastian Noack
2017/10/10 04:51:40
I don't see any change to the description of #5837
| |
253 | 219 |
254 if stringID in sourceData: | 220 if stringID in sourceData: |
255 if noMangling: | 221 key = re.sub(r'\W', '_', stringID) |
Sebastian Noack
2017/10/06 19:13:29
This should no longer be necessary either, as we w
tlucas
2017/10/09 23:18:07
A quick test verified your assumption, this line h
| |
256 key = re.sub(r'\W', '_', stringID) | |
257 else: | |
258 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) | |
259 if key in data: | 222 if key in data: |
260 print 'Warning: locale string %s defined multiple ti mes' % key | 223 print 'Warning: locale string %s defined multiple ti mes' % key |
261 | 224 |
262 import_string(data, key, sourceData[stringID]) | 225 data[key] = sourceData[stringID] |
263 except Exception as e: | 226 except Exception as e: |
264 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) | 227 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) |
265 | 228 |
266 files[targetFile] = toJson(data) | 229 files[targetFile] = toJson(data) |
267 | 230 |
268 | 231 |
269 def truncate(text, length_limit): | 232 def truncate(text, length_limit): |
270 if len(text) <= length_limit: | 233 if len(text) <= length_limit: |
271 return text | 234 return text |
272 return text[:length_limit - 1].rstrip() + u'\u2026' | 235 return text[:length_limit - 1].rstrip() + u'\u2026' |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 365 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
403 ) | 366 ) |
404 | 367 |
405 zipdata = files.zipToString() | 368 zipdata = files.zipToString() |
406 signature = None | 369 signature = None |
407 pubkey = None | 370 pubkey = None |
408 if keyFile != None: | 371 if keyFile != None: |
409 signature = signBinary(zipdata, keyFile) | 372 signature = signBinary(zipdata, keyFile) |
410 pubkey = getPublicKey(keyFile) | 373 pubkey = getPublicKey(keyFile) |
411 writePackage(outFile, pubkey, signature, zipdata) | 374 writePackage(outFile, pubkey, signature, zipdata) |
OLD | NEW |