mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added fix command to the i18n that allows to add missing translations to the another languages
This commit is contained in:
@@ -2,21 +2,26 @@ import os
|
||||
import sys
|
||||
|
||||
|
||||
def get_keys(elements):
|
||||
arr = []
|
||||
for el in elements:
|
||||
if el.startswith("//") or len(el) == 0 or el == "\n":
|
||||
def dictionary(lines):
|
||||
parsed_lines = {}
|
||||
for i, line in enumerate(lines):
|
||||
if line.startswith("//") or len(line) == 0 or line == "\n":
|
||||
continue
|
||||
key = el.replace("\n", "").split(" = ")[0].replace('"', "")
|
||||
arr.append(key)
|
||||
|
||||
return arr
|
||||
line = line.replace("\n", "")
|
||||
pair = line.split(" = ")
|
||||
parsed_lines[i] = {
|
||||
"key": pair[0].replace('"', ""),
|
||||
"value": pair[1].replace('"', "").replace(';', "")
|
||||
}
|
||||
return parsed_lines
|
||||
|
||||
|
||||
class i18n:
|
||||
path = os.getcwd() + "/Stats/Supporting Files/"
|
||||
|
||||
def __init__(self):
|
||||
if "Kit/scripts" in os.getcwd():
|
||||
self.path = os.getcwd() + "/../../Stats/Supporting Files/"
|
||||
self.languages = list(filter(lambda x: x.endswith(".lproj"), os.listdir(self.path)))
|
||||
|
||||
def en_file(self):
|
||||
@@ -27,26 +32,42 @@ class i18n:
|
||||
|
||||
def check(self):
|
||||
en_file = self.en_file()
|
||||
en_count = len(en_file)
|
||||
en_keys = get_keys(en_file)
|
||||
if len(en_keys) == 0:
|
||||
sys.exit("No English keys found.")
|
||||
en_dict = dictionary(en_file)
|
||||
|
||||
for lang in self.languages:
|
||||
file = open(f"{self.path}/{lang}/Localizable.strings", "r").readlines()
|
||||
name = lang.replace(".lproj", "")
|
||||
keys = get_keys(file)
|
||||
if len(keys) == 0:
|
||||
sys.exit(f"No {name} keys found.")
|
||||
lang_dict = dictionary(file)
|
||||
|
||||
for i, el in enumerate(keys):
|
||||
if el != en_keys[i]:
|
||||
sys.exit(f"missing or wrong key `{el}` in `{name}` on line `{i}`, must be `{en_keys[i]}`")
|
||||
for v in en_dict:
|
||||
en_key = en_dict[v].get("key")
|
||||
lang_ley = lang_dict[v].get("key")
|
||||
if lang_ley != en_key:
|
||||
sys.exit(f"missing or wrong key `{lang_ley}` in `{name}` on line `{v}`, must be `{en_key}`")
|
||||
|
||||
print(f"All fine, found {en_count} lines in {len(self.languages)} languages.")
|
||||
print(f"All fine, found {len(en_file)} lines in {len(self.languages)} languages.")
|
||||
|
||||
def fix(self):
|
||||
pass
|
||||
en_file = self.en_file()
|
||||
en_dict = dictionary(en_file)
|
||||
|
||||
for v in en_dict:
|
||||
en_key = en_dict[v].get("key")
|
||||
en_value = en_dict[v].get("value")
|
||||
|
||||
for lang in self.languages:
|
||||
lang_path = f"{self.path}/{lang}/Localizable.strings"
|
||||
file = open(lang_path, "r").readlines()
|
||||
lang_dict = dictionary(file)
|
||||
|
||||
if v not in lang_dict or en_key != lang_dict[v].get("key"):
|
||||
file.insert(v, f"\"{en_key}\" = \"{en_value}\";\n")
|
||||
with open(lang_path, "w") as f:
|
||||
file = "".join(file)
|
||||
f.write(file)
|
||||
f.close()
|
||||
|
||||
self.check()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user