Add trans-clean.py to remove old trans keys

pull/9355/head
Benedikt Werner 2021-07-05 23:58:56 +02:00
parent c17c254ca2
commit 0905cc6390
No known key found for this signature in database
GPG Key ID: 1DBFF0F8E9E121EB
1 changed files with 47 additions and 0 deletions

47
bin/trans-clean.py 100644
View File

@ -0,0 +1,47 @@
#!/usr/bin/env python3
"""
Remove old translation keys (i.e. ones without matching source keys)
from `translation/dest/*` to avoid failing trans lints until
the next crowdin pull.
"""
import sys
import pathlib
import xml.etree.ElementTree as ET
def clean(path):
db = path.parent.name # site, study, ...
source = ET.parse(path.parent.parent.parent / "source" / f"{db}.xml").getroot()
tree = ET.parse(path)
root = tree.getroot()
change = False
for el in list(root):
name = el.attrib["name"]
source_el = source.find(f".//{el.tag}[@name='{name}']")
if source_el is None:
root.remove(el)
change = True
if change:
if root:
last = root[-1]
last.tail = last.tail.rstrip(" ")
with open(path, "wb") as f:
f.write(b'<?xml version="1.0" encoding="utf-8"?>\n')
tree.write(f, xml_declaration=False, encoding="utf-8", short_empty_elements=False)
f.write(b"\n")
return change
if __name__ == "__main__":
args = sys.argv[1:]
if args:
changed = sum(clean(pathlib.Path(arg)) for arg in sys.argv[1:])
print("Changed", changed, "files")
else:
print(f"Usage: {sys.argv[0]} translation/dest/*/*.xml")