This repository was archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathupdate-i18n.py
executable file
·133 lines (93 loc) · 4.16 KB
/
update-i18n.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#! /usr/bin/python2.4
__copyright__ = '''\
Copyright (C) 2008 Geometer Plus <[email protected]>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
'''
import sys, os
from optparse import OptionParser
from elementtree.ElementTree import Element, SubElement, ElementTree
SPACES = '\t'
def fix_it(elem):
if len(elem):
for child in elem:
fix_it(child)
last = elem.getchildren()[-1]
last.tail = last.tail[:-len(SPACES)]
class Collector:
def __init__(self, dirname='.'):
self._dirname = dirname
self._resources = {}
self._resources_order = []
def process(self, lang):
assert len(lang) == 2, 'Language name must be two letters long'
doc = ElementTree(file=os.path.join(self._dirname, '%s.xml' % lang))
root = doc.getroot()
if root.tag == 'resources':
for child in root:
self.walk(child, (child.get('name'),), lang)
def walk(self, element, prefix, lang):
value = element.get('value', None)
if value is not None and element.get('toBeTranslated', None) is None:
if prefix not in self._resources:
tempo = {}
self._resources_order.append(prefix)
else:
tempo = self._resources[prefix]
tempo[lang] = value
self._resources[prefix] = tempo
for child in element:
self.walk(child, prefix + (child.get('name'), ), lang)
def dump(self, output, lang):
tempo = {}
root = Element('resources')
root.tail = '\n'
tempo[()] = root
for key in self._resources_order:
for i in range(1, len(key)+1):
if key[0:i] not in tempo:
parent = tempo[key[0:i-1]]
value = self._resources.get(key[0:i], None)
if value is None:
elem = SubElement(parent, 'node', name=key[i-1])
else:
localized = value.get(lang, None)
english = value.get('en', None)
if english is None:
print >> sys.stderr, 'English file does not have the string for', key[0:i]
print >> sys.stderr, ' entry is marked as obosolete.'
elem = SubElement(parent, 'node', name=key[i-1], value=localized, obsolete='true')
elif localized is not None:
elem = SubElement(parent, 'node', name=key[i-1], value=localized)
else:
elem = SubElement(parent, 'node', name=key[i-1], value=english, toBeTranslated='true')
parent.text = elem.tail = '\n' + i*SPACES
tempo[key[0:i]] = elem
fix_it(root)
print >> output, '<?xml version="1.0" encoding="UTF-8"?>'
ElementTree(root).write(output, 'utf-8')
def main():
'''actual worker'''
parser = OptionParser()
options, args = parser.parse_args()
#parser.add_option('-d', '--dir', dest='dirname', default='.', help='specify directory where files reside')
options.dirname = '.'
if len(args) == 0:
parser.error('Invalid number of arguments')
if not os.path.isdir(options.dirname):
parser.error('Directory "%s" does not exist' % options.dirname)
coll = Collector(options.dirname)
coll.process('en')
coll.process(args[0])
coll.dump(sys.stdout, args[0])
if __name__ == '__main__':
main()
# vim:ts=4:sw=4:et