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 parts = sourceFile.split(os.path.sep) |
222 locale = parts[-2].replace('-', '_') | 200 locale = parts[-2].replace('-', '_') |
223 targetFile = os.path.join('_locales', locale, 'messages.json') | 201 targetFile = os.path.join('_locales', locale, 'messages.json') |
224 data = json.loads(files.get(targetFile, '{}').decode('utf-8')) | 202 data = json.loads(files.get(targetFile, '{}').decode('utf-8')) |
225 | 203 |
226 try: | 204 try: |
227 # The WebExtensions (.json) and Gecko format provide | 205 with io.open(sourceFile, 'r', encoding='utf-8') as handle: |
228 # translations differently and/or provide additional | 206 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 | 207 |
240 # Resolve wildcard imports | 208 # Resolve wildcard imports |
241 if keys == '*' or keys == '=*': | 209 if keys == '*' or keys == '=*': |
242 importList = sourceData.keys() | 210 importList = sourceData.keys() |
243 importList = filter(lambda k: not k.startswith('_'), importL
ist) | 211 importList = filter(lambda k: not k.startswith('_'), importL
ist) |
244 if keys == '=*': | 212 if keys == '=*': |
245 importList = map(lambda k: '=' + k, importList) | 213 importList = map(lambda k: '=' + k, importList) |
246 keys = ' '.join(importList) | 214 keys = ' '.join(importList) |
247 | 215 |
248 for stringID in keys.split(): | 216 for stringID in keys.split(): |
249 noMangling = False | 217 noMangling = False |
250 if stringID.startswith('='): | 218 if stringID.startswith('='): |
251 stringID = stringID[1:] | 219 stringID = stringID[1:] |
252 noMangling = True | 220 noMangling = True |
253 | 221 |
254 if stringID in sourceData: | 222 if stringID in sourceData: |
255 if noMangling: | 223 if noMangling: |
256 key = re.sub(r'\W', '_', stringID) | 224 key = re.sub(r'\W', '_', stringID) |
257 else: | 225 else: |
258 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub(
r'\W', '_', stringID) | 226 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub(
r'\W', '_', stringID) |
259 if key in data: | 227 if key in data: |
260 print 'Warning: locale string %s defined multiple ti
mes' % key | 228 print 'Warning: locale string %s defined multiple ti
mes' % key |
261 | 229 |
262 import_string(data, key, sourceData[stringID]) | 230 data[key] = sourceData[stringID] |
263 except Exception as e: | 231 except Exception as e: |
264 print 'Warning: error importing locale data from %s: %s' % (sour
ceFile, e) | 232 print 'Warning: error importing locale data from %s: %s' % (sour
ceFile, e) |
265 | 233 |
266 files[targetFile] = toJson(data) | 234 files[targetFile] = toJson(data) |
267 | 235 |
268 | 236 |
269 def truncate(text, length_limit): | 237 def truncate(text, length_limit): |
270 if len(text) <= length_limit: | 238 if len(text) <= length_limit: |
271 return text | 239 return text |
272 return text[:length_limit - 1].rstrip() + u'\u2026' | 240 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') | 370 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
403 ) | 371 ) |
404 | 372 |
405 zipdata = files.zipToString() | 373 zipdata = files.zipToString() |
406 signature = None | 374 signature = None |
407 pubkey = None | 375 pubkey = None |
408 if keyFile != None: | 376 if keyFile != None: |
409 signature = signBinary(zipdata, keyFile) | 377 signature = signBinary(zipdata, keyFile) |
410 pubkey = getPublicKey(keyFile) | 378 pubkey = getPublicKey(keyFile) |
411 writePackage(outFile, pubkey, signature, zipdata) | 379 writePackage(outFile, pubkey, signature, zipdata) |
OLD | NEW |