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] |
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 == '*': |
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 == '=*': | |
245 importList = map(lambda k: '=' + k, importList) | |
246 keys = ' '.join(importList) | 211 keys = ' '.join(importList) |
247 | 212 |
248 for stringID in keys.split(): | 213 for stringID in keys.split(): |
249 noMangling = False | 214 if stringID in sourceData: |
250 if stringID.startswith('='): | 215 if stringID in data: |
251 stringID = stringID[1:] | 216 print ('Warning: locale string {} defined multiple' |
252 noMangling = True | 217 ' times').format(stringID) |
253 | 218 |
254 if stringID in sourceData: | 219 data[stringID] = sourceData[stringID] |
255 if noMangling: | |
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: | |
260 print 'Warning: locale string %s defined multiple ti
mes' % key | |
261 | |
262 import_string(data, key, sourceData[stringID]) | |
263 except Exception as e: | 220 except Exception as e: |
264 print 'Warning: error importing locale data from %s: %s' % (sour
ceFile, e) | 221 print 'Warning: error importing locale data from %s: %s' % (sour
ceFile, e) |
265 | 222 |
266 files[targetFile] = toJson(data) | 223 files[targetFile] = toJson(data) |
267 | 224 |
268 | 225 |
269 def truncate(text, length_limit): | 226 def truncate(text, length_limit): |
270 if len(text) <= length_limit: | 227 if len(text) <= length_limit: |
271 return text | 228 return text |
272 return text[:length_limit - 1].rstrip() + u'\u2026' | 229 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') | 359 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
403 ) | 360 ) |
404 | 361 |
405 zipdata = files.zipToString() | 362 zipdata = files.zipToString() |
406 signature = None | 363 signature = None |
407 pubkey = None | 364 pubkey = None |
408 if keyFile != None: | 365 if keyFile != None: |
409 signature = signBinary(zipdata, keyFile) | 366 signature = signBinary(zipdata, keyFile) |
410 pubkey = getPublicKey(keyFile) | 367 pubkey = getPublicKey(keyFile) |
411 writePackage(outFile, pubkey, signature, zipdata) | 368 writePackage(outFile, pubkey, signature, zipdata) |
OLD | NEW |