diff options
| author | Jules Aguillon | 2022-11-26 18:11:28 +0100 |
|---|---|---|
| committer | Jules Aguillon | 2022-11-26 18:11:28 +0100 |
| commit | ebdacbc2b2ac48e137dd9a1409aa334512e2fb62 (patch) | |
| tree | 549cfd7acf3fbe7730bc5de1d4e1e40117239808 /sync_translations.py | |
| parent | 122a9c23d01eaf8c65d7c72f5976049d25c3093a (diff) | |
| download | unexpected-keyboard-ebdacbc2b2ac48e137dd9a1409aa334512e2fb62.tar.gz unexpected-keyboard-ebdacbc2b2ac48e137dd9a1409aa334512e2fb62.zip | |
Make sure translation are in sync
The new script makes sure that strings files don't contain obsolete
strings but also ease the job of contributors by adding missing
translations as comments.
A Github Action ensures that translations stay in sync over time.
Diffstat (limited to 'sync_translations.py')
| -rw-r--r-- | sync_translations.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/sync_translations.py b/sync_translations.py new file mode 100644 index 0000000..b76b8d1 --- /dev/null +++ b/sync_translations.py @@ -0,0 +1,41 @@ +import xml.etree.ElementTree as ET +from xml.sax import saxutils +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): + resources = ET.parse(file).getroot() + return [ + ((ent.get("name"), ent.get("product")), ent.text) + for ent in resources if ent.tag == "string" + ] + +def string_entry_str(name, text, product): + product_attr = ' product="%s"' % product if product != None else "" + text_encoded = saxutils.escape(text) + return '<string name="%s"%s>%s</string>' % (name, product_attr, text_encoded) + +def write_updated_strings(out, baseline, strings): + out.write('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n') + for key, default_text in baseline: + name, product = key + if key in strings: + ent = string_entry_str(name, strings[key], product) + out.write(" %s\n" % ent) + else: + def_ent = string_entry_str(name, default_text, product) + out.write(" <!-- %s -->\n" % def_ent) + 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)) + with open(strings_file, "w") as out: + write_updated_strings(out, baseline, strings) |
