-
Notifications
You must be signed in to change notification settings - Fork 23
/
clean-xliff.py
executable file
·73 lines (62 loc) · 2.33 KB
/
clean-xliff.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /usr/bin/env python
#
# clean-xliff.py <l10n_folder> <xliff_file>
#
# Remove targets from a locale, remove target-language attribute
#
from glob import glob
from lxml import etree
import argparse
import os
NS = {'x':'urn:oasis:names:tc:xliff:document:1.2'}
def indent(elem, level=0):
# Prettify XML output
# http://effbot.org/zone/element-lib.htm#prettyprint
i = '\n' + level*' '
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + ' '
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def main():
parser = argparse.ArgumentParser()
parser.add_argument('l10n_folder', help='Path to locale folder to clean up')
parser.add_argument('xliff_file', help='Name of the XLIFF file, e.g. firefox-ios.xliff')
args = parser.parse_args()
xliff_filename = args.xliff_file
file_path = os.path.join(
os.path.realpath(args.l10n_folder),
xliff_filename
)
print 'Updating %s' % file_path
# Read localized file XML
locale_tree = etree.parse(file_path)
locale_root = locale_tree.getroot()
# Remove existing localizations and target-language
for trans_node in locale_root.xpath('//x:trans-unit', namespaces=NS):
for child in trans_node.xpath('./x:target', namespaces=NS):
child.getparent().remove(child)
# Remove target-language where defined
for file_node in locale_root.xpath('//x:file', namespaces=NS):
if file_node.get('target-language'):
file_node.attrib.pop('target-language')
# Replace the existing locale file with the new XML content
with open(file_path, 'w') as fp:
# Fix indentations
indent(locale_root)
xliff_content = etree.tostring(
locale_tree,
encoding='UTF-8',
xml_declaration=True,
pretty_print=True
)
fp.write(xliff_content)
if __name__ == '__main__':
main()