| 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 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 import_string_webext(data, key, source): | |
| 194 """Import a single translation from the source dictionary into data""" | |
| 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. | |
| 203 """ | |
| 204 match = re.search(r'^(.*?)\s*\(&.\)$', value) | |
| 205 if match: | |
| 206 value = match.group(1) | |
| 207 else: | |
| 208 index = value.find('&') | |
| 209 if index >= 0: | |
| 210 value = value[0:index] + value[index + 1:] | |
| 211 | |
| 212 data[key] = {'message': value} | |
| 213 | |
| 214 | |
| 193 def import_locales(params, files): | 215 def import_locales(params, files): |
| 194 import localeTools | 216 import localeTools |
| 195 | 217 |
| 196 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as | 218 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as |
| 197 # separator instead. | 219 # separator instead. |
| 198 convert_locale_code = lambda code: code.replace('-', '_') | 220 convert_locale_code = lambda code: code.replace('-', '_') |
| 199 | 221 |
| 200 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome | 222 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome |
| 201 # locales to themselves, merely with the dash as separator. | 223 # locales to themselves, merely with the dash as separator. |
| 202 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} | 224 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 220 fileName, keys = item | 242 fileName, keys = item |
| 221 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) | 243 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) |
| 222 sourceFile = os.path.join(os.path.dirname(item.source), *parts) | 244 sourceFile = os.path.join(os.path.dirname(item.source), *parts) |
| 223 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') | 245 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') |
| 224 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): | 246 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): |
| 225 continue | 247 continue |
| 226 | 248 |
| 227 data = json.loads(files[targetFile].decode('utf-8')) | 249 data = json.loads(files[targetFile].decode('utf-8')) |
| 228 | 250 |
| 229 try: | 251 try: |
| 230 # .json and other formats provide translations differently and | 252 # The WebExtensions (.json) and Gecko format provide |
| 231 # / or provide additional information like e.g. "placeholders". | 253 # translations differently and/or provide additional |
| 232 # We want to adhere to that / preserve the addtional info | 254 # information like e.g. "placeholders". We want to adhere to |
| 255 # that and preserve the addtional info. | |
| 233 if sourceFile.endswith('.json'): | 256 if sourceFile.endswith('.json'): |
| 234 with io.open(sourceFile, 'r', encoding='utf-8') as handle: | 257 with io.open(sourceFile, 'r', encoding='utf-8') as handle: |
| 235 sourceData = json.load(handle) | 258 sourceData = json.load(handle) |
| 236 | 259 import_string = import_string_webext |
| 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) | |
| 244 else: | 260 else: |
| 245 sourceData = localeTools.readFile(sourceFile) | 261 sourceData = localeTools.readFile(sourceFile) |
| 246 | 262 import_string = import_string_gecko |
| 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 | 263 |
| 263 # Resolve wildcard imports | 264 # Resolve wildcard imports |
| 264 if keys == '*' or keys == '=*': | 265 if keys == '*' or keys == '=*': |
| 265 importList = sourceData.keys() | 266 importList = sourceData.keys() |
| 266 importList = filter(lambda k: not k.startswith('_'), importL ist) | 267 importList = filter(lambda k: not k.startswith('_'), importL ist) |
| 267 if keys == '=*': | 268 if keys == '=*': |
| 268 importList = map(lambda k: '=' + k, importList) | 269 importList = map(lambda k: '=' + k, importList) |
| 269 keys = ' '.join(importList) | 270 keys = ' '.join(importList) |
| 270 | 271 |
| 271 for stringID in keys.split(): | 272 for stringID in keys.split(): |
| 272 noMangling = False | 273 noMangling = False |
| 273 if stringID.startswith('='): | 274 if stringID.startswith('='): |
| 274 stringID = stringID[1:] | 275 stringID = stringID[1:] |
| 275 noMangling = True | 276 noMangling = True |
| 276 | 277 |
| 277 if stringID in sourceData: | 278 if stringID in sourceData: |
| 278 if noMangling: | 279 if noMangling: |
| 279 key = re.sub(r'\W', '_', stringID) | 280 key = re.sub(r'\W', '_', stringID) |
| 280 else: | 281 else: |
| 281 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) | 282 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) |
| 282 if key in data: | 283 if key in data: |
| 283 print 'Warning: locale string %s defined multiple ti mes' % key | 284 print 'Warning: locale string %s defined multiple ti mes' % key |
| 284 | 285 |
| 285 value = get_value(sourceData, stringID) | 286 import_string(data, key, sourceData[stringID]) |
| 286 | |
| 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.
| |
| 288 except Exception as e: | 287 except Exception as e: |
| 289 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) |
| 290 | 289 |
| 291 files[targetFile] = toJson(data) | 290 files[targetFile] = toJson(data) |
| 292 | 291 |
| 293 | 292 |
| 294 def truncate(text, length_limit): | 293 def truncate(text, length_limit): |
| 295 if len(text) <= length_limit: | 294 if len(text) <= length_limit: |
| 296 return text | 295 return text |
| 297 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... | |
| 417 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 416 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
| 418 ) | 417 ) |
| 419 | 418 |
| 420 zipdata = files.zipToString() | 419 zipdata = files.zipToString() |
| 421 signature = None | 420 signature = None |
| 422 pubkey = None | 421 pubkey = None |
| 423 if keyFile != None: | 422 if keyFile != None: |
| 424 signature = signBinary(zipdata, keyFile) | 423 signature = signBinary(zipdata, keyFile) |
| 425 pubkey = getPublicKey(keyFile) | 424 pubkey = getPublicKey(keyFile) |
| 426 writePackage(outFile, pubkey, signature, zipdata) | 425 writePackage(outFile, pubkey, signature, zipdata) |
| LEFT | RIGHT |