forked from juandg/IDE_translations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n_template.py
executable file
·86 lines (77 loc) · 1.9 KB
/
i18n_template.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
#!/usr/bin/python
#vim:set fileencoding=utf-8 sw=2
#
# Remove unused catalog text lines from ".po" file.
#
template = 'Resources_en.po'
def unquote(s):
s = s.strip()
if s[0] != '"' or s[-1] != '"':
raise RuntimeError
return s[1:-1]
def read_po(fp):
st = 1
comment = key = value = rkey = rvalue = ''
for line in fp:
if line[0] == '#' or line.strip() == '':
if st == 2:
yield (comment, key, value, rkey, rvalue)
st = 1
comment = key = value = rkey = rvalue = ''
comment += line
elif line[0] == '"':
if st == 1:
key += unquote(line)
rkey += line
else:
value += unquote(line)
rvalue += line
elif line.startswith('msgid '):
st = 1
key = unquote(line[5:])
rkey = line
elif line.startswith('msgstr '):
st = 2
value = unquote(line[6:])
rvalue = line
else:
raise RuntimeError
if st == 2:
yield (comment, key, value, rkey, rvalue)
def process(target):
# Read the current text catalog.
d = {}
firstcomment = ''
it = read_po(file(target))
try:
(comment, key, value, rkey, rvalue) = it.next()
d[key] = rvalue
firstcomment = comment # Preserve the first comment block
except StopIteration:
pass
for (comment, key, value, rkey, rvalue) in it:
d[key] = rvalue
# Read the template text catalog and output.
# The translated values come from the current text catalog read above.
out = file(target, 'w')
out.write(firstcomment)
it = read_po(file(template))
try:
(comment, key, value, rkey, rvalue) = it.next()
out.write(rkey)
out.write(d.get(key, rvalue))
except StopIteration:
pass
for (comment, key, value, rkey, rvalue) in it:
out.write(comment)
out.write(rkey)
out.write(d.get(key, rvalue))
def main():
import sys
for f in sys.argv[1:]:
if f == template:
continue
print 'Update %s...' % f
process(f)
if __name__ == '__main__':
main()