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

Delta Between Two Patch Sets: localeTools.py

Issue 29587910: Issue 104 - added checktranslations function
Left Patch Set: Proposed changes Created Oct. 31, 2017, 4:28 a.m.
Right Patch Set: Proposed changes Created Nov. 20, 2017, 12:38 p.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 | « build.py ('k') | no next file » | 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 re 5 import re
6 import os 6 import os
7 import sys 7 import sys
8 import codecs 8 import codecs
9 import json 9 import json
10 import urlparse 10 import urlparse
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 continue 297 continue
298 for file in os.listdir(baseDir): 298 for file in os.listdir(baseDir):
299 path = os.path.join(baseDir, file) 299 path = os.path.join(baseDir, file)
300 valid_extension = file.endswith('.json') 300 valid_extension = file.endswith('.json')
301 if os.path.isfile(path) and valid_extension and not file in files: 301 if os.path.isfile(path) and valid_extension and not file in files:
302 os.remove(path) 302 os.remove(path)
303 303
304 304
305 def check_translations(localeConfig): 305 def check_translations(localeConfig):
306 base_path = localeConfig['base_path'] 306 base_path = localeConfig['base_path']
307 json_fld = path.rsplit('/', 1)[1]
308 for locale in os.listdir(base_path): 307 for locale in os.listdir(base_path):
309 jsonpath = os.path.join(json_fld, locale, 'messages.json') 308 jsonpath = os.path.join(base_path, locale, 'messages.json')
310 309
311 if not os.path.exists(jsonpath): 310 if not os.path.exists(jsonpath):
312 continue 311 continue
313 312
314 with codecs.open(jsonpath, 'rb', encoding='utf-8') as f: 313 with codecs.open(jsonpath, 'rb', encoding='utf-8') as f:
315 try: 314 try:
316 data = json.load(f) 315 data = json.load(f)
317 except ValueError, e: 316 for key, value in data.iteritems():
318 print >>sys.stderr, '%s -> File is not valid.' + e 317 variables = set()
tlucas 2017/10/31 07:43:10 ? Does this even work? I am expecting either .form
erick 2017/11/06 06:29:44 Done.
319 318 max_length = value.get('maxLength')
320 for key, value in data.iteritems(): 319 length = len(value['message'])
321 variables = set() 320 if (max_length is not None and
322 max_length = value.get('maxLength') 321 length > max_length or length > 160):
323 length = len(value['message']) 322 print >>sys.stderr, (
324 if max_length is not None and length > max_length or length > 160: 323 '{} {} -> translation might be too long.'
325 print >>sys.stderr, ( 324 'More then 160 chars or maxLength exceeded'
326 '{} {} -> translation might be too long. More then 160 ' 325 ).format(locale, key)
327 'chars or maxLength exceeded').format(locale, key) 326
328 327 for match in re.finditer(r'\$(\S+?)\$', value['message']):
329 for match in re.finditer(r'\$(\S+?)\$', value['message']): 328 variables.add(match.group(1))
330 variables.add(match.group(1)) 329 expected = set(value.get('placeholders', {}).iterkeys())
331 expected = set(value.get('placeholders', {}).iterkeys()) 330 if variables != expected:
332 if variables != expected: 331 print >>sys.stderr, (
333 print >>sys.stderr, ( 332 '{} {}: Variables used are {} '
334 '{} {}: Variables used are {} ' 333 'expected. Variables: {}'
335 'expected. Variables: {}' 334 ).format(locale, key, variables, expected)
336 ).format(locale, key, variables, expected) 335 except ValueError:
336 print >>sys.stderr, locale + ', messages.json is not valid.'
LEFTRIGHT
« build.py ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld