Skip to content

Instantly share code, notes, and snippets.

@neotrinity
Last active August 29, 2015 14:01
Show Gist options
  • Save neotrinity/38d5e60035e8ecdbadc4 to your computer and use it in GitHub Desktop.
Save neotrinity/38d5e60035e8ecdbadc4 to your computer and use it in GitHub Desktop.
import sys
## Typical Usage:
## python parse_transalations.py 'path/to/messages.po'
## This prints the strings and their respective translations
def clean(s):
""" Quick and dirty scrubbing of the data """
return s.replace('"', '').replace('msgid ', '').replace('msgstr ', '')
def parse():
filename = sys.argv[1] # input .po file
# slurp the contents
f = open(filename, 'rb')
string = f.read()
f.close()
lines = string.split('\n')
d = {}
key = ''
value = ''
for line in lines:
if line.startswith('msgid'):
# start of a key
d[clean(key)] = clean(value)
key=line
value=''
elif key and line.startswith('"'):
# multiline apppend to key or value
if value:
value+=line
else:
key+=line
elif line.startswith('msgstr'):
#start of value
value=line
# pop in the last value
d[clean(key)] = clean(value)
for k, v in sorted(d.items(), key=lambda x: x[0].lower()):
print k + "===" + v
if __name__ == '__main__':
parse()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment