5 def parse_translation(f
):
6 """Read a single translation entry from the file F and return a
7 tuple with the comments, msgid and msgstr. The comments is returned
8 as a list of lines which do not end in new-lines. The msgid and
9 msgstr are strings, possibly with embedded newlines"""
15 if line
.strip() == '':
16 return comments
, None, None
18 comments
.append(line
[:-1])
24 if line
[:7] != 'msgid "' or line
[-2] != '"':
25 raise RuntimeError("parse error")
31 msgid
+= '\n' + line
[:-1]
34 if line
[:8] != 'msgstr "' or line
[-2] != '"':
35 raise RuntimeError("parse error")
39 if len(line
) == 0 or line
[0] != '"':
41 msgstr
+= '\n' + line
[:-1]
43 if line
.strip() != '':
44 raise RuntimeError("parse error")
46 return comments
, msgid
, msgstr
48 def split_comments(comments
):
49 """Split COMMENTS into flag comments and other comments. Flag
50 comments are those that begin with '#,', e.g. '#,fuzzy'."""
54 if len(c
) > 1 and c
[1] == ',':
62 argv0
= os
.path
.basename(argv
[0])
63 sys
.exit('Usage: %s <lang.po>\n'
65 'This script will replace the translations and flags in lang.po with\n'
66 'the translations and flags in the source po file read from standard\n'
67 'input. Strings that are not found in the source file are left untouched.\n'
68 'A backup copy of lang.po is saved as lang.po.bak.\n'
71 ' svn cat http://svn.collab.net/repos/svn/trunk/subversion/po/sv.po | \\\n'
72 ' %s sv.po' % (argv0
, argv0
))
74 # Read the source po file into a hash
77 comments
, msgid
, msgstr
= parse_translation(sys
.stdin
)
78 if not comments
and msgid
is None:
81 source
[msgid
] = msgstr
, split_comments(comments
)[0]
83 # Make a backup of the output file, open the copy for reading
84 # and the original for writing.
85 os
.rename(argv
[1], argv
[1] + '.bak')
86 infile
= open(argv
[1] + '.bak')
87 outfile
= open(argv
[1], 'w')
89 # Loop thought the original and replace stuff as we go
95 comments
, msgid
, msgstr
= parse_translation(infile
)
96 if not comments
and msgid
is None:
102 outfile
.write('\n'.join(comments
) + '\n')
105 # Do not update the header, and only update if the source
106 # has a non-empty translation.
107 if msgid
!= '""' and source
.get(msgid
, ['""', []])[0] != '""':
108 other
= split_comments(comments
)[1]
109 new_msgstr
, new_flags
= source
[msgid
]
110 new_comments
= other
+ new_flags
111 if new_msgstr
!= msgstr
or new_comments
!= comments
:
114 comments
= new_comments
115 outfile
.write('\n'.join(comments
) + '\n')
116 outfile
.write('msgid ' + msgid
+ '\n')
117 outfile
.write('msgstr ' + msgstr
+ '\n')
121 # We're done. Tell the user what we did.
122 print('%d strings updated. '
123 '%d of %d strings are still untranslated (%.0f%%).' %
124 (update_count
, untranslated
, string_count
,
125 100.0 * untranslated
/ string_count
))
127 if __name__
== '__main__':