Skip to content

Commit d5203f5

Browse files
author
Shigeru KANEMOTO
committed
Sort the key of *.po files.
Without sorting the key, it is difficult to maintain the language resource files on Git.
1 parent 5b65ae0 commit d5203f5

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

app/src/processing/app/i18n/i18n_update.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ def read_po(fp):
3030
rvalue += line
3131
elif line.startswith('msgid '):
3232
st = 1
33-
key = unquote(line[5:])
33+
key = unquote(line[6:])
3434
rkey = line
3535
elif line.startswith('msgstr '):
3636
st = 2
37-
value = unquote(line[6:])
37+
value = unquote(line[7:])
3838
rvalue = line
3939
else:
4040
raise RuntimeError
@@ -45,34 +45,29 @@ def read_po(fp):
4545
def main():
4646
import sys
4747

48-
# Read the current text catalog.
48+
# Read the new text catalog template.
4949
d = {}
50-
firstcomment = ''
51-
it = read_po(file(sys.argv[1]))
52-
try:
53-
(comment, key, value, rkey, rvalue) = it.next()
54-
d[key] = rvalue
55-
firstcomment = comment # Preserve the first comment block
56-
except StopIteration:
57-
pass
58-
for (comment, key, value, rkey, rvalue) in it:
59-
d[key] = rvalue
50+
for (comment, key, value, rkey, rvalue) in read_po(sys.stdin):
51+
d[key] = (comment, rkey, rvalue)
52+
53+
# Override existing entries with current text catalog.
54+
for (comment, key, value, rkey, rvalue) in read_po(file(sys.argv[1])):
55+
if d.has_key(key):
56+
d[key] = (comment, rkey, rvalue)
6057

61-
# Read the new text catalog template and output.
62-
# The translated values come from the current text catalog read above.
6358
out = file(sys.argv[1], 'w')
64-
out.write(firstcomment)
65-
it = read_po(sys.stdin)
66-
try:
67-
(comment, key, value, rkey, rvalue) = it.next()
59+
if d.has_key(''):
60+
(comment, rkey, rvalue) = d['']
61+
out.write(comment)
6862
out.write(rkey)
69-
out.write(d.get(key, rvalue))
70-
except StopIteration:
71-
pass
72-
for (comment, key, value, rkey, rvalue) in it:
63+
out.write(rvalue)
64+
del d['']
65+
66+
for key in sorted(d.keys()):
67+
(comment, rkey, rvalue) = d[key]
7368
out.write(comment)
7469
out.write(rkey)
75-
out.write(d.get(key, rvalue))
70+
out.write(rvalue)
7671

7772
if __name__ == '__main__':
7873
main()

0 commit comments

Comments
 (0)