Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: packagerChrome.py

Issue 29517660: Issue 5477 - Import everything from imported locales (Closed)
Left Patch Set: Adressing comment from Sebastian Created Aug. 18, 2017, 7:32 a.m.
Right Patch Set: Created Aug. 22, 2017, 7:59 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | packagerEdge.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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
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 return data[stringID]
249
250 def set_translation(data, key, value, *args):
251 data[key] = {'message': value}
252 263
253 # Resolve wildcard imports 264 # Resolve wildcard imports
254 if keys == '*' or keys == '=*': 265 if keys == '*' or keys == '=*':
255 importList = sourceData.keys() 266 importList = sourceData.keys()
256 importList = filter(lambda k: not k.startswith('_'), importL ist) 267 importList = filter(lambda k: not k.startswith('_'), importL ist)
257 if keys == '=*': 268 if keys == '=*':
258 importList = map(lambda k: '=' + k, importList) 269 importList = map(lambda k: '=' + k, importList)
259 keys = ' '.join(importList) 270 keys = ' '.join(importList)
260 271
261 for stringID in keys.split(): 272 for stringID in keys.split():
262 noMangling = False 273 noMangling = False
263 if stringID.startswith('='): 274 if stringID.startswith('='):
264 stringID = stringID[1:] 275 stringID = stringID[1:]
265 noMangling = True 276 noMangling = True
266 277
267 if stringID in sourceData: 278 if stringID in sourceData:
268 if noMangling: 279 if noMangling:
269 key = re.sub(r'\W', '_', stringID) 280 key = re.sub(r'\W', '_', stringID)
270 else: 281 else:
271 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) 282 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID)
272 if key in data: 283 if key in data:
273 print 'Warning: locale string %s defined multiple ti mes' % key 284 print 'Warning: locale string %s defined multiple ti mes' % key
274 285
275 # Remove access keys 286 import_string(data, key, sourceData[stringID])
276 value = get_value(sourceData, stringID)
277 match = re.search(r'^(.*?)\s*\(&.\)$', value)
278 if match:
279 value = match.group(1)
280 else:
281 index = value.find('&')
282 if index >= 0:
283 value = value[0:index] + value[index + 1:]
284
285 set_translation(data, key, value, sourceData, stringID)
286 except Exception as e: 287 except Exception as e:
287 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)
288 289
289 files[targetFile] = toJson(data) 290 files[targetFile] = toJson(data)
290 291
291 292
292 def truncate(text, length_limit): 293 def truncate(text, length_limit):
293 if len(text) <= length_limit: 294 if len(text) <= length_limit:
294 return text 295 return text
295 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
415 params, 'testIndex.html.tmpl', ('general', 'testScripts') 416 params, 'testIndex.html.tmpl', ('general', 'testScripts')
416 ) 417 )
417 418
418 zipdata = files.zipToString() 419 zipdata = files.zipToString()
419 signature = None 420 signature = None
420 pubkey = None 421 pubkey = None
421 if keyFile != None: 422 if keyFile != None:
422 signature = signBinary(zipdata, keyFile) 423 signature = signBinary(zipdata, keyFile)
423 pubkey = getPublicKey(keyFile) 424 pubkey = getPublicKey(keyFile)
424 writePackage(outFile, pubkey, signature, zipdata) 425 writePackage(outFile, pubkey, signature, zipdata)
LEFTRIGHT
« no previous file | packagerEdge.py » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld