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

Unified Diff: localeTools.py

Issue 29587910: Issue 104 - added checktranslations function
Patch Set: Proposed changes Created Oct. 31, 2017, 4:28 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « build.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: localeTools.py
diff --git a/localeTools.py b/localeTools.py
index b6554e0aa52461a7347c843a3b251d035ec34652..aa5f87b167d8a3477aa69729723721704936d754 100644
--- a/localeTools.py
+++ b/localeTools.py
@@ -300,3 +300,37 @@ def getTranslations(localeConfig, projectName, key):
valid_extension = file.endswith('.json')
if os.path.isfile(path) and valid_extension and not file in files:
os.remove(path)
+
+
+def check_translations(localeConfig):
+ base_path = localeConfig['base_path']
+ json_fld = path.rsplit('/', 1)[1]
+ for locale in os.listdir(base_path):
+ jsonpath = os.path.join(json_fld, locale, 'messages.json')
+
+ if not os.path.exists(jsonpath):
+ continue
+
+ with codecs.open(jsonpath, 'rb', encoding='utf-8') as f:
+ try:
+ data = json.load(f)
+ except ValueError, e:
+ print >>sys.stderr, '%s -> File is not valid.' + e
tlucas 2017/10/31 07:43:10 ? Does this even work? I am expecting either .form
erick 2017/11/06 06:29:44 Done.
+
+ for key, value in data.iteritems():
+ variables = set()
+ max_length = value.get('maxLength')
+ length = len(value['message'])
+ if max_length is not None and length > max_length or length > 160:
+ print >>sys.stderr, (
+ '{} {} -> translation might be too long. More then 160 '
+ 'chars or maxLength exceeded').format(locale, key)
+
+ for match in re.finditer(r'\$(\S+?)\$', value['message']):
+ variables.add(match.group(1))
+ expected = set(value.get('placeholders', {}).iterkeys())
+ if variables != expected:
+ print >>sys.stderr, (
+ '{} {}: Variables used are {} '
+ 'expected. Variables: {}'
+ ).format(locale, key, variables, expected)
« no previous file with comments | « build.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld