Skip to content

Instantly share code, notes, and snippets.

@ysc3839
Last active April 9, 2017 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysc3839/90979dfbf0943457b487d7ca64459b91 to your computer and use it in GitHub Desktop.
Save ysc3839/90979dfbf0943457b487d7ca64459b91 to your computer and use it in GitHub Desktop.
PO Msg Converter
import sys
from os import path
import re
import polib
def msg2po(msgfile, pofile):
with open(msgfile, 'rb') as f:
messages = f.read()
po = polib.POFile()
p = re.compile(r'\["([^"]*)"\]="([^"]*)"')
for m in p.finditer(messages):
g = m.groups()
po.append(polib.POEntry(
msgctxt=g[0].decode('ascii'),
msgid=g[1].decode('utf-8')
))
po.save(pofile)
if __name__ == '__main__':
if len(sys.argv) <= 1:
print 'Usage: %s msgfile [pofile]' % path.basename(sys.argv[0])
else:
msgfile = sys.argv[1]
pofile = None
if len(sys.argv) > 2: pofile = sys.argv[2]
if pofile == None: pofile = path.splitext(path.basename(msgfile))[0] + '.pot'
msg2po(msgfile, pofile)
import sys
from os import path
import polib
def po2msg(pofile, msgfile):
po = polib.pofile(pofile)
with open(msgfile, 'wb') as f:
f.write('dictionary=(\n')
for entry in po:
if entry.msgctxt and not entry.obsolete:
value = entry.msgstr or entry.msgid
f.write((u' ["' + entry.msgctxt + u'"]="' + value + u'"\n').encode('utf-8'))
f.write(')\n')
if __name__ == '__main__':
if len(sys.argv) <= 1:
print 'Usage: %s pofile [msgfile]' % path.basename(sys.argv[0])
else:
pofile = sys.argv[1]
msgfile = None
if len(sys.argv) > 2: msgfile = sys.argv[2]
if msgfile == None: msgfile = path.splitext(path.basename(pofile))[0]
po2msg(pofile, msgfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment