zulip/tools/i18n/process-mobile-i18n

54 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import re
from subprocess import check_output
import orjson
def get_json_filename(locale: str) -> str:
return f"locale/{locale}/mobile.json"
def get_locales() -> list[str]:
output = check_output(["git", "ls-files", "locale"], text=True)
tracked_files = output.split()
regex = re.compile(r"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_stats(resource_path: str) -> dict[str, int]:
with open(resource_path, "rb") as raw_resource_file:
raw_info = orjson.loads(raw_resource_file.read())
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_stats: dict[str, dict[str, int]] = {}
locale_paths = [] # List[str]
for locale in get_locales():
path = get_json_filename(locale)
if os.path.exists(path):
stats = get_translation_stats(path)
translation_stats.update({locale: stats})
locale_paths.append(path)
stats_path = os.path.join("locale", "mobile_info.json")
with open(stats_path, "wb") as f:
f.write(
orjson.dumps(
translation_stats,
option=orjson.OPT_APPEND_NEWLINE | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS,
)
)
print("Mobile stats file created at: " + stats_path)