in tools/dev/po-merge.py [0:0]
def main(argv):
if len(argv) != 2:
argv0 = os.path.basename(argv[0])
sys.exit('Usage: %s <lang.po>\n'
'\n'
'This script will replace the translations and flags in lang.po (LF line endings)\n'
'with the translations and flags in the source po file read from standard input.\n'
'Strings that are not found in the source file are left untouched.\n'
'A backup copy of lang.po is saved as lang.po.bak.\n'
'\n'
'Example:\n'
' svn cat http://svn.apache.org/repos/asf/subversion/trunk/subversion/po/sv.po | \\\n'
' %s sv.po' % (argv0, argv0))
# Read the source po file into a hash
source = {}
while True:
comments, msgid, msgid_plural, msgstr = parse_translation(sys.stdin)
if not comments and msgid is None:
break
if msgid is not None:
source[msgid] = msgstr, split_comments(comments)[0]
# Make a backup of the output file, open the copy for reading
# and the original for writing.
os.rename(argv[1], argv[1] + '.bak')
infile = open(argv[1] + '.bak')
outfile = open(argv[1], 'w')
# Loop thought the original and replace stuff as we go
first = 1
string_count = 0
update_count = 0
untranslated = 0
fuzzy = 0
while True:
comments, msgid, msgid_plural, msgstr = parse_translation(infile)
if not comments and msgid is None:
break
if not first:
outfile.write('\n')
first = 0
if msgid is None:
outfile.write('\n'.join(comments) + '\n')
else:
string_count += 1
# Do not update the header, and only update if the source
# has a non-empty translation.
if msgid != '""' and source.get(msgid, ['""', []])[0] != '""':
other = split_comments(comments)[1]
new_msgstr, new_flags = source[msgid]
new_comments = other + new_flags
if new_msgstr != msgstr or new_comments != comments:
update_count += 1
msgstr = new_msgstr
comments = new_comments
outfile.write('\n'.join(comments) + '\n')
outfile.write('msgid ' + msgid + '\n')
if not msgid_plural:
outfile.write('msgstr ' + msgstr[0] + '\n')
else:
outfile.write('msgid_plural ' + msgid_plural + '\n')
n = 0
for i in msgstr:
outfile.write('msgstr[%s] %s\n' % (n, msgstr[n]))
n += 1
if msgstr is not None:
for m in msgstr:
if m == '""':
untranslated += 1
for c in comments:
if c.startswith('#,') and 'fuzzy' in c.split(', '):
fuzzy += 1
# We're done. Tell the user what we did.
print(('%d strings updated. '
'%d fuzzy strings. '
'%d of %d strings are still untranslated (%.0f%%).' %
(update_count, fuzzy, untranslated, string_count,
100.0 * untranslated / string_count)))