2022-11-26 18:11:28 +01:00
|
|
|
import xml.etree.ElementTree as ET
|
2024-02-05 17:36:43 +01:00
|
|
|
import glob, os
|
2022-11-26 18:11:28 +01:00
|
|
|
|
|
|
|
# Edit every strings.xml files:
|
|
|
|
# - Add missing translation as comments
|
|
|
|
# - Remove obsolete strings
|
|
|
|
# - Sort in the same order as the baseline
|
|
|
|
# The baseline is 'values/strings.xml', which is english.
|
2024-03-04 00:32:47 +01:00
|
|
|
# Sync store title and descriptions to the 'metadata/' directory.
|
|
|
|
|
|
|
|
VALUE_DIR_TO_METADATA = {
|
|
|
|
"cs": "cs-CZ",
|
|
|
|
"de": "de-DE",
|
|
|
|
"en": "en-US",
|
|
|
|
"es": "es-ES",
|
|
|
|
"fa": "fa-IR",
|
|
|
|
"fr": "fr-FR",
|
|
|
|
"it": "it-IT",
|
|
|
|
"ko": "ko-KR",
|
|
|
|
"lv": "lv",
|
|
|
|
"pl": "pl-PL",
|
|
|
|
"pt": "pt-BR",
|
|
|
|
"ro": "ro",
|
|
|
|
"ru": "ru-RU",
|
|
|
|
"tr": "tr-TR",
|
|
|
|
"uk": "uk",
|
|
|
|
"vi": "vi",
|
|
|
|
"zh-rCN": "zh-CN",
|
|
|
|
}
|
2022-11-26 18:11:28 +01:00
|
|
|
|
2023-08-05 17:27:56 +02:00
|
|
|
# Dict of strings. Key is the pair string name and product field (often None).
|
2022-11-26 18:11:28 +01:00
|
|
|
def parse_strings_file(file):
|
2023-03-28 11:11:41 +02:00
|
|
|
def key(ent): return ent.get("name"), ent.get("product")
|
|
|
|
resrcs = ET.parse(file).getroot()
|
|
|
|
return { key(ent): ent for ent in resrcs if ent.tag == "string" }
|
2022-11-26 18:11:28 +01:00
|
|
|
|
2023-08-05 17:27:56 +02:00
|
|
|
# Print the XML file back autoformatted. Takes the output of [sync].
|
|
|
|
def write_updated_strings(out, strings):
|
2022-11-26 18:11:28 +01:00
|
|
|
out.write('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n')
|
2023-08-05 17:27:56 +02:00
|
|
|
for key, string, comment in strings:
|
|
|
|
out.write(" ")
|
|
|
|
if comment: out.write("<!-- ")
|
|
|
|
out.write(ET.tostring(string, "unicode").strip())
|
|
|
|
if comment: out.write(" -->")
|
|
|
|
out.write("\n")
|
2022-11-26 18:11:28 +01:00
|
|
|
out.write('</resources>\n')
|
|
|
|
|
2023-08-05 17:27:56 +02:00
|
|
|
# Print whether string file is uptodate.
|
|
|
|
def print_status(fname, strings):
|
|
|
|
# Number of commented-out strings
|
|
|
|
c = sum(1 for _, _, comment in strings if comment)
|
|
|
|
status = "uptodate" if c == 0 else "missing %d strings" % c
|
|
|
|
print("%s: %s" % (fname, status))
|
|
|
|
|
|
|
|
# Returns a list of tuples (key, string, commented).
|
|
|
|
def sync(baseline, strings):
|
|
|
|
return [
|
|
|
|
(key, strings[key], False) if key in strings else
|
|
|
|
(key, base_string, True)
|
|
|
|
for key, base_string in baseline.items() ]
|
|
|
|
|
2024-03-04 00:32:47 +01:00
|
|
|
def sync_metadata(value_dir, strings):
|
2024-03-04 00:54:17 +01:00
|
|
|
if "short_description" not in strings:
|
|
|
|
return # Short description is mandatory, do nothing without it.
|
2024-03-04 00:32:47 +01:00
|
|
|
locale = os.path.basename(value_dir).removeprefix("values-")
|
|
|
|
if not locale in VALUE_DIR_TO_METADATA:
|
|
|
|
raise Exception("Locale '%s' not known, please add it into sync_translations.py" % locale)
|
|
|
|
meta_dir = "metadata/android/" + VALUE_DIR_TO_METADATA[locale]
|
2024-02-05 17:36:43 +01:00
|
|
|
def sync_meta_file(fname, string_name):
|
|
|
|
if string_name in strings:
|
|
|
|
string = strings[string_name]
|
|
|
|
if not os.path.isdir(meta_dir):
|
|
|
|
os.makedirs(meta_dir)
|
|
|
|
txt_file = os.path.join(meta_dir, fname)
|
|
|
|
with open(txt_file, "w", encoding="utf-8") as out:
|
|
|
|
out.write(string.text.removeprefix('"').removesuffix('"'))
|
|
|
|
out.write("\n")
|
|
|
|
sync_meta_file("title.txt", ("app_name_release", None))
|
|
|
|
sync_meta_file("short_description.txt", ("short_description", None))
|
|
|
|
sync_meta_file("full_description.txt", ("store_description", None))
|
|
|
|
|
2022-11-26 18:11:28 +01:00
|
|
|
baseline = parse_strings_file("res/values/strings.xml")
|
|
|
|
|
2024-02-05 17:36:43 +01:00
|
|
|
for value_dir in glob.glob("res/values-*"):
|
|
|
|
strings_file = os.path.join(value_dir, "strings.xml")
|
|
|
|
if os.path.isfile(strings_file):
|
|
|
|
local_strings = dict(parse_strings_file(strings_file))
|
|
|
|
synced_strings = sync(baseline, local_strings)
|
|
|
|
with open(strings_file, "w", encoding="utf-8") as out:
|
|
|
|
write_updated_strings(out, synced_strings)
|
2024-03-04 00:32:47 +01:00
|
|
|
sync_metadata(value_dir, local_strings)
|
2024-02-05 17:36:43 +01:00
|
|
|
print_status(strings_file, synced_strings)
|
|
|
|
|
|
|
|
sync_metadata("en", baseline)
|