Rewrite reomve-unused-trans in python
This makes it no longer depend on lint, and also adds support for arrays
This commit is contained in:
parent
8c7a821e8d
commit
7bb9615355
57
F-Droid/tools/remove-unused-trans.py
Executable file
57
F-Droid/tools/remove-unused-trans.py
Executable file
@ -0,0 +1,57 @@
|
||||
#!/bin/env python2
|
||||
|
||||
# Remove extra translations
|
||||
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
from xml.etree import ElementTree
|
||||
|
||||
strings = set()
|
||||
arrays = set()
|
||||
|
||||
for e in ElementTree.parse(os.path.join('res', 'values', 'strings.xml')).getroot().findall('.//string'):
|
||||
name = e.attrib['name']
|
||||
strings.add(name)
|
||||
|
||||
for e in ElementTree.parse(os.path.join('res', 'values', 'array.xml')).getroot().findall('.//string-array'):
|
||||
name = e.attrib['name']
|
||||
arrays.add(name)
|
||||
|
||||
for d in glob.glob(os.path.join('res', 'values-*')):
|
||||
|
||||
str_path = os.path.join(d, 'strings.xml')
|
||||
if os.path.exists(str_path):
|
||||
tree = ElementTree.parse(str_path)
|
||||
root = tree.getroot()
|
||||
|
||||
elems = root.findall('.//string')
|
||||
for e in elems:
|
||||
name = e.attrib['name']
|
||||
if name not in strings:
|
||||
root.remove(e)
|
||||
|
||||
result = re.sub(r' />', r'/>', ElementTree.tostring(root, encoding='utf-8'))
|
||||
|
||||
with open(str_path, 'w+') as f:
|
||||
f.write('<?xml version="1.0" encoding="utf-8"?>\n')
|
||||
f.write(result)
|
||||
f.write('\n')
|
||||
|
||||
arr_path = os.path.join(d, 'array.xml')
|
||||
if os.path.exists(arr_path):
|
||||
tree = ElementTree.parse(arr_path)
|
||||
root = tree.getroot()
|
||||
|
||||
elems = root.findall('.//string-array')
|
||||
for e in elems:
|
||||
name = e.attrib['name']
|
||||
if name not in arrays:
|
||||
root.remove(e)
|
||||
|
||||
result = re.sub(r' />', r'/>', ElementTree.tostring(root, encoding='utf-8'))
|
||||
|
||||
with open(arr_path, 'w+') as f:
|
||||
f.write('<?xml version="1.0" encoding="utf-8"?>\n')
|
||||
f.write(result)
|
||||
f.write('\n')
|
@ -1,12 +0,0 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
# Remove extra translations
|
||||
|
||||
sed -n 's@.*res/values-[^/]\+/\([^\.]\+\)\.xml:.*Error: "\([^"]*\)" is translated here but not found in default locale.*@\1 \2@p' < build/outputs/lint-results.txt | \
|
||||
while read file name; do
|
||||
if [[ $file == strings ]]; then
|
||||
sed -i "/name=\"$name\"/d" res/values-*/strings.xml
|
||||
elif [[ $file == array ]]; then
|
||||
sed -i "/<string-array name=\"$name\"/,/<\/string-array/d" res/values-*/array.xml
|
||||
fi
|
||||
done
|
Loading…
x
Reference in New Issue
Block a user