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

Side by Side Diff: localeTools.py

Issue 29690867: Issue 104 - Added translation checking Base URL: https://hg.adblockplus.org/buildtools/file/097b8d9c43a8
Patch Set: Created Feb. 6, 2018, 2:22 p.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 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 # Remove any extra files 318 # Remove any extra files
319 for dir, files in dirs.iteritems(): 319 for dir, files in dirs.iteritems():
320 baseDir = os.path.join(localeConfig['base_path'], dir) 320 baseDir = os.path.join(localeConfig['base_path'], dir)
321 if not os.path.exists(baseDir): 321 if not os.path.exists(baseDir):
322 continue 322 continue
323 for file in os.listdir(baseDir): 323 for file in os.listdir(baseDir):
324 path = os.path.join(baseDir, file) 324 path = os.path.join(baseDir, file)
325 valid_extension = file.endswith('.json') 325 valid_extension = file.endswith('.json')
326 if os.path.isfile(path) and valid_extension and not file in files: 326 if os.path.isfile(path) and valid_extension and not file in files:
327 os.remove(path) 327 os.remove(path)
328
329
330 def check_translations(localeConfig):
331 base_path = localeConfig['base_path']
332 for locale in os.listdir(base_path):
333 jsonpath = os.path.join(base_path, locale, 'messages.json')
334
335 if not os.path.exists(jsonpath):
336 continue
337
338 with codecs.open(jsonpath, 'rb', encoding='utf-8') as f:
339 try:
340 data = json.load(f)
341 for key, value in data.iteritems():
342 variables = set()
343 max_length = value.get('maxLength')
344 length = len(value['message'])
345 if (max_length is not None and
346 length > max_length or length > 160):
347 print >>sys.stderr, (
348 '{} {} -> translation might be too long.'
349 'More then 160 chars or maxLength exceeded'
350 ).format(locale, key)
351
352 for match in re.finditer(r'\$(\S+?)\$', value['message']):
353 variables.add(match.group(1))
354 expected = set(value.get('placeholders', {}).iterkeys())
355 if variables != expected:
356 print >>sys.stderr, (
357 '{} {}: Variables used are {} '
358 'expected. Variables: {}'
359 ).format(locale, key, variables, expected)
360 except ValueError:
361 print >>sys.stderr, locale + ', messages.json is not valid.'
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