| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 | 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 import_string_webext(data, key, source): | 193 def import_string_webext(data, key, source): |
| 194 """Sets data[key] to the desired {'message':value}, adds all additional | 194 """Import a single translation from the source dictionary into data""" |
| 195 info from the source dict | 195 data[key] = source |
| 196 | |
| 197 | |
| 198 def import_string_gecko(data, key, value): | |
| 199 """Import Gecko-style locales into data. | |
| 200 | |
| 201 Only sets {'message': value} in the data-dictionary, after stripping | |
| 202 undesired Gecko-style access keys. | |
| 196 """ | 203 """ |
| 197 value = source['message'] | |
| 198 | |
| 199 data[key] = {'messages': value} | |
| 200 for k, v in source.items(): | |
| 201 data[key].setdefault(k, v) | |
| 202 | |
| 203 | |
| 204 def import_string_gecko(data, key, value): | |
| 205 """only set {'message': value} in data-dictionary, after stripping | |
|
Sebastian Noack
2017/08/18 15:26:14
You capitalized the above docstring but not this o
tlucas
2017/08/18 15:34:18
Done. Also added full-stop to the above docstring.
| |
| 206 undesired gecko-style access keys | |
| 207 """ | |
| 208 | |
| 209 # Remove access keys from possible gecko-style | |
| 210 # translations | |
| 211 match = re.search(r'^(.*?)\s*\(&.\)$', value) | 204 match = re.search(r'^(.*?)\s*\(&.\)$', value) |
| 212 if match: | 205 if match: |
| 213 value = match.group(1) | 206 value = match.group(1) |
| 214 else: | 207 else: |
| 215 index = value.find('&') | 208 index = value.find('&') |
| 216 if index >= 0: | 209 if index >= 0: |
| 217 value = value[0:index] + value[index + 1:] | 210 value = value[0:index] + value[index + 1:] |
| 218 | 211 |
| 219 data[key] = {'message': value} | 212 data[key] = {'message': value} |
| 220 | 213 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 249 fileName, keys = item | 242 fileName, keys = item |
| 250 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) | 243 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) |
| 251 sourceFile = os.path.join(os.path.dirname(item.source), *parts) | 244 sourceFile = os.path.join(os.path.dirname(item.source), *parts) |
| 252 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') | 245 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') |
| 253 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): | 246 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): |
| 254 continue | 247 continue |
| 255 | 248 |
| 256 data = json.loads(files[targetFile].decode('utf-8')) | 249 data = json.loads(files[targetFile].decode('utf-8')) |
| 257 | 250 |
| 258 try: | 251 try: |
| 259 # The WebExtensions (.json) and Gecko format provide | 252 # The WebExtensions (.json) and Gecko format provide |
|
Sebastian Noack
2017/08/18 15:26:14
There is a redundant space in this line.
tlucas
2017/08/18 15:34:17
Done.
| |
| 260 # translations differently and/or provide additional | 253 # translations differently and/or provide additional |
| 261 # information like e.g. "placeholders". We want to adhere to | 254 # information like e.g. "placeholders". We want to adhere to |
| 262 # that and preserve the addtional info | 255 # that and preserve the addtional info. |
|
Sebastian Noack
2017/08/18 15:26:14
Missing full-stop at end of sentence.
tlucas
2017/08/18 15:34:17
Done.
| |
| 263 | |
| 264 if sourceFile.endswith('.json'): | 256 if sourceFile.endswith('.json'): |
| 265 with io.open(sourceFile, 'r', encoding='utf-8') as handle: | 257 with io.open(sourceFile, 'r', encoding='utf-8') as handle: |
| 266 sourceData = json.load(handle) | 258 sourceData = json.load(handle) |
| 267 | 259 import_string = import_string_webext |
| 268 set_translation = import_string_webext | |
|
Sebastian Noack
2017/08/18 15:26:14
For consistency you might want to rename the set_t
tlucas
2017/08/18 15:34:17
Done.
| |
| 269 else: | 260 else: |
| 270 sourceData = localeTools.readFile(sourceFile) | 261 sourceData = localeTools.readFile(sourceFile) |
| 271 | 262 import_string = import_string_gecko |
| 272 set_translation = import_string_gecko | |
| 273 | 263 |
| 274 # Resolve wildcard imports | 264 # Resolve wildcard imports |
| 275 if keys == '*' or keys == '=*': | 265 if keys == '*' or keys == '=*': |
| 276 importList = sourceData.keys() | 266 importList = sourceData.keys() |
| 277 importList = filter(lambda k: not k.startswith('_'), importL ist) | 267 importList = filter(lambda k: not k.startswith('_'), importL ist) |
| 278 if keys == '=*': | 268 if keys == '=*': |
| 279 importList = map(lambda k: '=' + k, importList) | 269 importList = map(lambda k: '=' + k, importList) |
| 280 keys = ' '.join(importList) | 270 keys = ' '.join(importList) |
| 281 | 271 |
| 282 for stringID in keys.split(): | 272 for stringID in keys.split(): |
| 283 noMangling = False | 273 noMangling = False |
| 284 if stringID.startswith('='): | 274 if stringID.startswith('='): |
| 285 stringID = stringID[1:] | 275 stringID = stringID[1:] |
| 286 noMangling = True | 276 noMangling = True |
| 287 | 277 |
| 288 if stringID in sourceData: | 278 if stringID in sourceData: |
| 289 if noMangling: | 279 if noMangling: |
| 290 key = re.sub(r'\W', '_', stringID) | 280 key = re.sub(r'\W', '_', stringID) |
| 291 else: | 281 else: |
| 292 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) | 282 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) |
| 293 if key in data: | 283 if key in data: |
| 294 print 'Warning: locale string %s defined multiple ti mes' % key | 284 print 'Warning: locale string %s defined multiple ti mes' % key |
| 295 | 285 |
| 296 set_translation(data, key, sourceData[stringID]) | 286 import_string(data, key, sourceData[stringID]) |
| 297 except Exception as e: | 287 except Exception as e: |
| 298 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) | 288 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) |
| 299 | 289 |
| 300 files[targetFile] = toJson(data) | 290 files[targetFile] = toJson(data) |
| 301 | 291 |
| 302 | 292 |
| 303 def truncate(text, length_limit): | 293 def truncate(text, length_limit): |
| 304 if len(text) <= length_limit: | 294 if len(text) <= length_limit: |
| 305 return text | 295 return text |
| 306 return text[:length_limit - 1].rstrip() + u'\u2026' | 296 return text[:length_limit - 1].rstrip() + u'\u2026' |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 426 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 416 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
| 427 ) | 417 ) |
| 428 | 418 |
| 429 zipdata = files.zipToString() | 419 zipdata = files.zipToString() |
| 430 signature = None | 420 signature = None |
| 431 pubkey = None | 421 pubkey = None |
| 432 if keyFile != None: | 422 if keyFile != None: |
| 433 signature = signBinary(zipdata, keyFile) | 423 signature = signBinary(zipdata, keyFile) |
| 434 pubkey = getPublicKey(keyFile) | 424 pubkey = getPublicKey(keyFile) |
| 435 writePackage(outFile, pubkey, signature, zipdata) | 425 writePackage(outFile, pubkey, signature, zipdata) |
| LEFT | RIGHT |