Created
February 18, 2014 09:25
-
-
Save elprup/9067484 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/python | |
import subprocess as spb | |
import os.path | |
import sys | |
msgfmt_path = '/tmp/msgfmt.py' | |
def run(cmd): | |
print 'func[cmd]:' + cmd | |
sock = spb.Popen(cmd, shell=True, stdout=spb.PIPE) | |
return sock.stdout.read() | |
def get_pot(file_path, domain, charset='utf-8', encoding='utf-8'): | |
cmd = 'pygettext ' + file_path | |
print run(cmd) | |
cmd = 'sed -i \'s/charset=CHARSET/charset=%s/g\' %s.pot' % (charset, domain) | |
print run(cmd) | |
cmd = 'sed -i \'s/Encoding: ENCODING/Encoding: %s/g\' %s.pot' % (encoding, domain) | |
print run(cmd) | |
def gen_locales(potfile, langs=[], root='./locales', update=False): | |
for lang in langs: | |
p = os.path.join(root, lang, 'LC_MESSAGES') | |
cmd = 'mkdir -p ' + p | |
print run(cmd) | |
potfilename = os.path.split(potfile)[-1] | |
pofilename = potfilename[:-1] | |
if update==False or update=='False': | |
cmd = 'cp %s %s' % (potfile, os.path.join(p, pofilename)) | |
else: | |
cmd = 'msgmerge --update %s %s' % (os.path.join(p, pofilename), potfile) | |
print run(cmd) | |
def compile_po(root='.'): | |
cmd = 'find %s -name *.po |xargs %s' % (root, msgfmt_path) | |
print run(cmd) | |
def usage(): | |
usage = '''i18n_task_py [cmd] [options] | |
pot: source_root domain [charset encoding] | |
generate pot file in current directory. | |
genlocale: potfile langs(seperated by ,) root | |
generate locales directory | |
updatelocale: potfile langs(seperated by ,) root | |
update locales directory | |
compile: [root] | |
compile po files to mo | |
example: | |
# initial | |
./i18n_task.py pot main.py messages | |
./i18n_task.py genlocale ./messages.pot zh_CN,en ./locales | |
./i18n_task.py compile ./locales | |
# update | |
./i18n_task.py pot main.py messages | |
./i18n_task.py updatelocale ./messages.pot zh_CN,en ./locales | |
./i18n_task.py compile ./locales | |
''' | |
print usage | |
if __name__ == '__main__': | |
len_arg = len(sys.argv) | |
if len_arg <2: | |
usage() | |
sys.exit(0) | |
command = sys.argv[1] | |
if command == 'pot': | |
get_pot(*sys.argv[2:]) | |
elif command == 'genlocale': | |
mlangs, root = [], './locales' | |
if len_arg >= 4: mlangs = sys.argv[3].split(',') | |
if len_arg >= 5: root = sys.argv[4] | |
gen_locales(sys.argv[2], mlangs, root, update=False) | |
elif command == 'updatelocale': | |
mlangs, root = [], './locales' | |
if len_arg >= 4: mlangs = sys.argv[3].split(',') | |
if len_arg >= 5: root = sys.argv[4] | |
gen_locales(sys.argv[2], mlangs, root, update=True) | |
elif command == 'compile': | |
root = '.' | |
if len_arg >= 3: | |
root = (sys.argv[2]) | |
compile_po(root) | |
else: | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment