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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 ).encode('utf-8') | 183 ).encode('utf-8') |
184 | 184 |
185 | 185 |
186 def toJson(data): | 186 def toJson(data): |
187 return json.dumps( | 187 return json.dumps( |
188 data, ensure_ascii=False, sort_keys=True, | 188 data, ensure_ascii=False, sort_keys=True, |
189 indent=2, separators=(',', ': ') | 189 indent=2, separators=(',', ': ') |
190 ).encode('utf-8') + '\n' | 190 ).encode('utf-8') + '\n' |
191 | 191 |
192 | 192 |
193 def importGeckoLocales(params, files): | 193 def import_locales(params, files): |
194 import localeTools | 194 import localeTools |
195 | 195 |
196 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as | 196 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as |
197 # separator instead. | 197 # separator instead. |
198 convert_locale_code = lambda code: code.replace('-', '_') | 198 convert_locale_code = lambda code: code.replace('-', '_') |
199 | 199 |
200 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome | 200 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome |
201 # locales to themselves, merely with the dash as separator. | 201 # locales to themselves, merely with the dash as separator. |
202 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} | 202 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} |
203 | 203 |
(...skipping 16 matching lines...) Expand all Loading... | |
220 fileName, keys = item | 220 fileName, keys = item |
221 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) | 221 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) |
222 sourceFile = os.path.join(os.path.dirname(item.source), *parts) | 222 sourceFile = os.path.join(os.path.dirname(item.source), *parts) |
223 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') | 223 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') |
224 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): | 224 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): |
225 continue | 225 continue |
226 | 226 |
227 data = json.loads(files[targetFile].decode('utf-8')) | 227 data = json.loads(files[targetFile].decode('utf-8')) |
228 | 228 |
229 try: | 229 try: |
230 # .json and other formats provide translations differently and | |
231 # / or provide additional information like e.g. "placeholders". | |
232 # We want to adhere to that / preserve the addtional info | |
230 if sourceFile.endswith('.json'): | 233 if sourceFile.endswith('.json'): |
231 with io.open(sourceFile, 'r', encoding='utf-8') as handle: | 234 with io.open(sourceFile, 'r', encoding='utf-8') as handle: |
232 sourceData = {k: v['message'] for k, v in json.load(hand le).iteritems()} | 235 sourceData = json.load(handle) |
236 | |
237 def get_value(data, stringID): | |
238 return data[stringID]['message'] | |
239 | |
240 def set_translation(data, key, value, source, stringID): | |
241 data[key] = {'messages': value} | |
242 for k, v in source[stringID].items(): | |
243 data[key].setdefault(k, v) | |
233 else: | 244 else: |
234 sourceData = localeTools.readFile(sourceFile) | 245 sourceData = localeTools.readFile(sourceFile) |
235 | 246 |
247 def get_value(data, stringID): | |
248 # Remove access keys from possible gecko-style | |
249 # translations | |
250 value = data[stringID] | |
251 match = re.search(r'^(.*?)\s*\(&.\)$', value) | |
252 if match: | |
253 value = match.group(1) | |
254 else: | |
255 index = value.find('&') | |
256 if index >= 0: | |
257 value = value[0:index] + value[index + 1:] | |
258 return value | |
259 | |
260 def set_translation(data, key, value, *args): | |
261 data[key] = {'message': value} | |
262 | |
236 # Resolve wildcard imports | 263 # Resolve wildcard imports |
237 if keys == '*' or keys == '=*': | 264 if keys == '*' or keys == '=*': |
238 importList = sourceData.keys() | 265 importList = sourceData.keys() |
239 importList = filter(lambda k: not k.startswith('_'), importL ist) | 266 importList = filter(lambda k: not k.startswith('_'), importL ist) |
240 if keys == '=*': | 267 if keys == '=*': |
241 importList = map(lambda k: '=' + k, importList) | 268 importList = map(lambda k: '=' + k, importList) |
242 keys = ' '.join(importList) | 269 keys = ' '.join(importList) |
243 | 270 |
244 for stringID in keys.split(): | 271 for stringID in keys.split(): |
245 noMangling = False | 272 noMangling = False |
246 if stringID.startswith('='): | 273 if stringID.startswith('='): |
247 stringID = stringID[1:] | 274 stringID = stringID[1:] |
248 noMangling = True | 275 noMangling = True |
249 | 276 |
250 if stringID in sourceData: | 277 if stringID in sourceData: |
251 if noMangling: | 278 if noMangling: |
252 key = re.sub(r'\W', '_', stringID) | 279 key = re.sub(r'\W', '_', stringID) |
253 else: | 280 else: |
254 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) | 281 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) |
255 if key in data: | 282 if key in data: |
256 print 'Warning: locale string %s defined multiple ti mes' % key | 283 print 'Warning: locale string %s defined multiple ti mes' % key |
257 | 284 |
258 # Remove access keys | 285 value = get_value(sourceData, stringID) |
259 value = sourceData[stringID] | 286 |
260 match = re.search(r'^(.*?)\s*\(&.\)$', value) | 287 set_translation(data, key, value, sourceData, stringID) |
Sebastian Noack
2017/08/18 13:54:48
It seems the logic could be further simplified by
tlucas
2017/08/18 14:35:42
Done.
| |
261 if match: | |
262 value = match.group(1) | |
263 else: | |
264 index = value.find('&') | |
265 if index >= 0: | |
266 value = value[0:index] + value[index + 1:] | |
267 data[key] = {'message': value} | |
268 except Exception as e: | 288 except Exception as e: |
269 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) | 289 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) |
270 | 290 |
271 files[targetFile] = toJson(data) | 291 files[targetFile] = toJson(data) |
272 | 292 |
273 | 293 |
274 def truncate(text, length_limit): | 294 def truncate(text, length_limit): |
275 if len(text) <= length_limit: | 295 if len(text) <= length_limit: |
276 return text | 296 return text |
277 return text[:length_limit - 1].rstrip() + u'\u2026' | 297 return text[:length_limit - 1].rstrip() + u'\u2026' |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 if metadata.has_section('convert_js'): | 393 if metadata.has_section('convert_js'): |
374 convertJS(params, files) | 394 convertJS(params, files) |
375 | 395 |
376 if metadata.has_section('preprocess'): | 396 if metadata.has_section('preprocess'): |
377 files.preprocess( | 397 files.preprocess( |
378 [f for f, _ in metadata.items('preprocess')], | 398 [f for f, _ in metadata.items('preprocess')], |
379 {'needsExt': True} | 399 {'needsExt': True} |
380 ) | 400 ) |
381 | 401 |
382 if metadata.has_section('import_locales'): | 402 if metadata.has_section('import_locales'): |
383 importGeckoLocales(params, files) | 403 import_locales(params, files) |
384 | 404 |
385 files['manifest.json'] = createManifest(params, files) | 405 files['manifest.json'] = createManifest(params, files) |
386 if type == 'chrome': | 406 if type == 'chrome': |
387 fixTranslationsForCWS(files) | 407 fixTranslationsForCWS(files) |
388 | 408 |
389 if devenv: | 409 if devenv: |
390 import buildtools | 410 import buildtools |
391 import random | 411 import random |
392 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js') | 412 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js') |
393 files['devenvVersion__'] = str(random.random()) | 413 files['devenvVersion__'] = str(random.random()) |
394 | 414 |
395 if metadata.has_option('general', 'testScripts'): | 415 if metadata.has_option('general', 'testScripts'): |
396 files['qunit/index.html'] = createScriptPage( | 416 files['qunit/index.html'] = createScriptPage( |
397 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 417 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
398 ) | 418 ) |
399 | 419 |
400 zipdata = files.zipToString() | 420 zipdata = files.zipToString() |
401 signature = None | 421 signature = None |
402 pubkey = None | 422 pubkey = None |
403 if keyFile != None: | 423 if keyFile != None: |
404 signature = signBinary(zipdata, keyFile) | 424 signature = signBinary(zipdata, keyFile) |
405 pubkey = getPublicKey(keyFile) | 425 pubkey = getPublicKey(keyFile) |
406 writePackage(outFile, pubkey, signature, zipdata) | 426 writePackage(outFile, pubkey, signature, zipdata) |
OLD | NEW |