abouttreesummaryrefslogcommitdiff
path: root/sync_translations.py
blob: 97c0ecc1b13a3784b82c748b3cbc4e5d0ffaeb65 (plain)
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
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):
    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" }

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")

def write_updated_strings(out, baseline, strings):
    out.write('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n')
    for key, baseline_entry in baseline.items():
        if key in strings:
            dump_entry(out, strings[key], False)
        else:
            dump_entry(out, baseline_entry, True)
    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", encoding="utf-8") as out:
        write_updated_strings(out, baseline, strings)