i18n: Add script to process mobile locales.

This script iterates over all the mobile.json resources and creates a
single file at static/locale/mobile_info.json which contains total and
not-translated strings information against each language. After doing
this, it deletes all the mobile i18n resources downloaded by
tools/sync-translations because we neither want to check them in our
repository nor we want to make our repository dirty.
This commit is contained in:
Umair Khan 2017-12-22 13:09:55 +05:00 committed by Tim Abbott
parent 151b3e9f1a
commit 8b33c46165
1 changed files with 45 additions and 0 deletions

45
scripts/process-mobile-i18n Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/env python3
import json
import os
import re
from subprocess import check_output
from typing import Dict, List
def get_json_filename(locale: str) -> str:
return "static/locale/{}/mobile.json".format(locale)
def get_locales() -> List[str]:
tracked_files = check_output(['git', 'ls-files', 'static/locale'])
tracked_files = tracked_files.decode().split()
regex = re.compile('static/locale/(\w+)/LC_MESSAGES/django.po')
locales = ['en']
for tracked_file in tracked_files:
matched = regex.search(tracked_file)
if matched:
locales.append(matched.group(1))
return locales
def get_translation_info(resource_path: str) -> Dict[str, int]:
with open(resource_path) as raw_resource_file:
raw_info = json.load(raw_resource_file)
total = len(raw_info)
not_translated = len([i for i in raw_info.items() if i[1] == ''])
return {'total': total, 'not_translated': not_translated}
translation_info = {} # type: Dict[str, Dict[str, int]]
locale_paths = [] # List[str]
for locale in get_locales():
path = get_json_filename(locale)
if os.path.exists(path):
info = get_translation_info(path)
translation_info.update({locale: info})
locale_paths.append(path)
resource_file_path = os.path.join('static', 'locale', 'mobile_info.json')
with open(resource_file_path, 'w') as f:
json.dump(translation_info, f, indent=2, sort_keys=True)
f.write('\n')
print("Mobile resource file created at: " + resource_file_path)