forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-missing-locales.py
60 lines (47 loc) · 2.2 KB
/
find-missing-locales.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
from collections import defaultdict
from glob import glob
from json import load
from os import path
ALL_PATH = "../../website/public/locales/**/*.json"
DIR = path.dirname(__file__)
EN_PATH = "../../website/public/locales/en/*.json"
def get_not_translated(en_json, translation_json, parent_key=None):
not_translated = []
for key in en_json.keys():
if key in translation_json and translation_json[key] == en_json[key]:
not_translated.append((f"{parent_key}.{key}" if parent_key else key))
elif isinstance(en_json[key], dict):
if key not in translation_json:
msg = f"{parent_key}.{key} (and children)" if parent_key else "{key} (and children)"
not_translated.append(msg)
else:
not_translated.extend(get_not_translated(en_json[key], translation_json[key], key))
return not_translated
def get_missing(en_json, translation_json):
return [key for key in en_json.keys() if key not in translation_json]
def print_result(missing, not_translated, file):
if len(missing):
print(f"[{path.basename(path.dirname(file))}] - {path.basename(file)}\tmissing: {missing}")
if len(not_translated):
print(
f"[{path.basename(path.dirname(file))}] - {path.basename(file)}\tpotentially untranslated: {not_translated}"
)
def audit(file, en_file):
en_json = load(open(en_file, encoding="utf-8"))
translation_json = load(open(file, encoding="utf-8"))
return (get_missing(en_json, translation_json), get_not_translated(en_json, translation_json), file)
def main():
per_language_dict = defaultdict(list)
for en_file in glob(path.join(DIR, EN_PATH)):
for file in glob(path.join(DIR, ALL_PATH)):
if path.basename(en_file) == path.basename(file) and file != en_file:
lang = path.basename(path.dirname(file))
if len(sys.argv) == 0 or lang in sys.argv:
file_info = audit(file, en_file)
per_language_dict[lang].append(file_info)
for results in per_language_dict.values():
list(map(lambda args: print_result(*args), results))
print()
if __name__ == "__main__":
main()