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

Side by Side Diff: localeTools.py

Issue 29587910: Issue 104 - added checktranslations function
Patch Set: Proposed changes Created Oct. 28, 2017, 4:21 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 # Remove any extra files 293 # Remove any extra files
294 for dir, files in dirs.iteritems(): 294 for dir, files in dirs.iteritems():
295 baseDir = os.path.join(localeConfig['base_path'], dir) 295 baseDir = os.path.join(localeConfig['base_path'], dir)
296 if not os.path.exists(baseDir): 296 if not os.path.exists(baseDir):
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
304
305 def check_translations(localeConfig):
306 path = localeConfig['base_path']
307 json_fld = path.rsplit('/', 1)[1]
tlucas 2017/10/29 23:10:04 Use os.path.split() to split paths. a) This will
erick 2017/10/31 04:30:02 I receive "AttributeError: 'tuple' object has no a
tlucas 2017/10/31 07:43:09 The error message pointed you to passing a tuple,
erick 2017/11/06 06:29:43 Done.
308 for locale in os.listdir('_locales/'):
tlucas 2017/10/29 23:10:04 As pointed in out in the last comment '_locales' i
erick 2017/10/31 04:30:02 Done.
309 jsonpath = os.path.join('_locales/', locale, 'messages.json')
310
311 if not os.path.exists(jsonpath):
312 continue
313
314 with codecs.open(jsonpath, 'rb', encoding='utf-8') as f:
315 try:
316 data = json.load(f)
317 except ValueError, e:
318 print >>sys.stderr, '%s -> File is not valid. %s' % e
319
320 for key, value in data.iteritems():
321 variables = set()
322 max_length = value.get('maxLength')
323 length = len(value['message'])
324 if max_length is not None and length > max_length or length > 160:
325 print >>sys.stderr, (
326 '{} {} -> translation might be too long. More then 160 '
327 'chars or maxLength exceeded').format(locale, key)
328
329 for match in re.finditer(r'\$(\S+?)\$', value['message']):
330 variables.add(match.group(1))
331 expected = set(value.get('placeholders', {}).iterkeys())
332 if variables != expected:
333 print >>sys.stderr, (
tlucas 2017/10/29 23:10:04 nit: this is rather awkward to read. maybe someth
erick 2017/10/31 04:30:02 Done.
334 '{} {}: Variables used are {} '
335 'expected. Variables: {}').format(
336 locale, key, variables, expected)
OLDNEW
« no previous file with comments | « build.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld