Created
November 9, 2010 12:26
-
-
Save malthe/669027 to your computer and use it in GitHub Desktop.
Fixes translation punctuation for a gettext .po file and writes it back to disk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
import sys | |
import StringIO | |
f = open(sys.argv[1], 'r') | |
g = iter(f) | |
default = None | |
out = StringIO.StringIO() | |
print ">>> Fixing: %s..." % f.name | |
for line in g: | |
line = line.rstrip() | |
out.write(line + "\n") | |
default = line.startswith('#. Default') and line or default | |
if line.startswith('msgid'): | |
if default is not None: | |
line = default | |
default = None | |
line = line.rstrip() | |
if not line.endswith('"'): | |
continue | |
line = line[:-1].rstrip(' ') | |
msgstr = next(g).rstrip() | |
# check for non-standard punctuation | |
if '。' in msgstr or ':' in msgstr: | |
print ' Aborting: Non-standard punctuation found: "。".' | |
sys.exit(0) | |
if not msgstr.endswith('"'): | |
out.write(msgstr + '"\n') | |
continue | |
msgstr = msgstr[:-1].rstrip(' ') | |
# skip empty | |
if msgstr.endswith('"'): | |
out.write(msgstr + '"\n') | |
continue | |
# missing colon | |
if line.endswith(':') and not msgstr.endswith(':'): | |
msgstr = msgstr.rstrip(' .,:;') + ':' | |
# missing literal ellipsis | |
elif line.endswith('…') and not msgstr.endswith('…'): | |
msgstr = msgstr.rstrip(' .,:;') + '…' | |
# missing ellipsis | |
elif line.endswith('...') and not msgstr.endswith('...'): | |
msgstr = msgstr.rstrip(' .,:;') + '...' | |
# missing punctuation | |
elif line.endswith('.') and msgstr.rstrip('.!:;') == msgstr: | |
msgstr = msgstr.rstrip(' ,-') + '.' | |
# no punctuation -- strip off any punctuation off translation | |
elif line.rstrip('.!:;') == line: | |
msgstr = msgstr.rstrip(' .!:;,') | |
out.write(msgstr + '"\n') | |
f.close() | |
open(sys.argv[1], 'w').write(out.getvalue()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment