2022-11-26 18:11:28 +01:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import glob
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
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-03-28 11:11:41 +02:00
|
|
|
def dump_entry(out, entry, comment):
|
|
|
|
out.write(" ")
|
|
|
|
if comment: out.write("<!-- ")
|
|
|
|
out.write(ET.tostring(entry, "unicode").strip())
|
|
|
|
if comment: out.write(" -->")
|
|
|
|
out.write("\n")
|
2022-11-26 18:11:28 +01:00
|
|
|
|
|
|
|
def write_updated_strings(out, baseline, strings):
|
|
|
|
out.write('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n')
|
2023-03-28 11:11:41 +02:00
|
|
|
for key, baseline_entry in baseline.items():
|
2022-11-26 18:11:28 +01:00
|
|
|
if key in strings:
|
2023-03-28 11:11:41 +02:00
|
|
|
dump_entry(out, strings[key], False)
|
2022-11-26 18:11:28 +01:00
|
|
|
else:
|
2023-03-28 11:11:41 +02:00
|
|
|
dump_entry(out, baseline_entry, True)
|
2022-11-26 18:11:28 +01:00
|
|
|
out.write('</resources>\n')
|
|
|
|
|
|
|
|
baseline = parse_strings_file("res/values/strings.xml")
|
|
|
|
|
|
|
|
for strings_file in glob.glob("res/values-*/strings.xml"):
|
|
|
|
print(strings_file)
|
|
|
|
strings = dict(parse_strings_file(strings_file))
|
2023-06-07 18:14:41 +02:00
|
|
|
with open(strings_file, "w", encoding="utf-8") as out:
|
2022-11-26 18:11:28 +01:00
|
|
|
write_updated_strings(out, baseline, strings)
|