#!/usr/bin/env python __requires__="TurboGears" """ Script to Handle the own Transifex translation """ import sys import subprocess import os import getopt import glob import shutil import turbogears from transifex.util import header, load_config, available_languages, get_modules, get_repositories # Global config vars domain = 'transifex' locale_dir = 'po/' poData = 'data' poView = 'view' poData_path = locale_dir + '/' + poData + '/' poView_path = locale_dir + '/' + poView + '/' def usage(name): print '''Transifex i18n tool. Easily handle the translations of the application. Usage: %(name)s [OPTIONS...] Options: -c, --compile\t\tCompile PO files for languages enabled in LINGUAS and make them available to the web interface -m, --merge\t\tMerge PO files with the POT one. -l, --collect\t\tCollect the messages of app and put them into a POT file ''' % {'name': name} def i18n_collect(): # Collect the messages and put them into the POT file subprocess.call(['/usr/bin/pybabel', 'extract', '-o', poView_path + domain + '.pot', '-F', './babel.cfg', 'transifex']) # Put into the POT the descriptions and summaries of all modules and repos registered # in the database, so it can be translatable from babel.messages.pofile import read_po, write_po, Catalog #catalog = read_po(file(locale_dir + domain + '.pot', 'r')) catalog = Catalog() for module in get_modules(): if module.description: catalog.add(module.description.replace('\r','')) if module.summary: catalog.add(module.summary.replace('\r','')) for repo in get_repositories(): if repo.description: catalog.add(repo.description.replace('\r','')) if repo.summary: catalog.add(repo.summary.replace('\r','')) write_po(file(poData_path + domain + '.pot', 'w'), catalog) def i18n_merge(): # View poFiles = filter(os.path.isfile, glob.glob(poView_path + '*.po')) # Merge all langs availables in LINGUAS for pofile in poFiles: lang = os.path.basename(pofile).rsplit('.', 1)[0] potFile = poView_path + domain + '.pot' poFile = poView_path + pofile + '.po' print '''Merging the view messages for %(lang)s language with the POT file...''' % {'lang': lang} subprocess.call(['/usr/bin/msgmerge', pofile, potFile,'-o', pofile]) # Database poFiles = filter(os.path.isfile, glob.glob(poData_path + '*.po')) # Merge all langs availables in LINGUAS for pofile in poFiles: lang = os.path.basename(pofile).rsplit('.', 1)[0] potFile = poData_path + domain + '.pot' poFile = poData_path + pofile + '.po' print '''Merging the database messages for %(lang)s language with the POT file...''' % {'lang': lang} subprocess.call(['/usr/bin/msgmerge', pofile, potFile,'-o', pofile]) print '\n' def i18n_compile(): # Delete all diretories langs diretories=[f for f in os.listdir(locale_dir) if os.path.isdir(locale_dir + f)] for d in diretories: if d!=poView and d!=poData: shutil.rmtree(locale_dir + d) # We need to have the pot file into the locale_dir for the `tg-admin i18n add` works subprocess.call(['/bin/cp',poView_path + domain + '.pot', locale_dir]) # Compile all langs availables in LINGUAS for lang in available_languages(poView_path): dirname = locale_dir + lang + '/LC_MESSAGES/' #if not os.path.isdir(dirname): os.makedirs(dirname) subprocess.call(['/usr/bin/tg-admin','i18n','add',lang]) os.remove(dirname + domain + '.po') mofile = dirname + domain + '.mo' pofile = poView_path + lang + '.po' pofiledata = poData_path + lang + '.po' if os.path.isfile(pofiledata): subprocess.call(['/usr/bin/msgcat','-n' , pofile, pofiledata, '-o', dirname + lang + '.po']) pofile = dirname + lang + '.po' subprocess.call(['/usr/bin/msgfmt', pofile, '-o', mofile]) print '''Compiling %(lang)s language...\n''' % {'lang': lang} # Delete the pot file from locale_dir subprocess.call(['/bin/rm','-rf' ,locale_dir + domain + '.pot']) def main(): try: opts, args = getopt.getopt(sys.argv[1:], "cml", ["compile", "merge", "collect"]) except getopt.GetoptError: usage(sys.argv[0]) sys.exit(2) configfile = None i18ncompile = False i18nmerge = False i18ncollect = False for o, a in opts: if o in ("-h", "--help"): usage(sys.argv[0]) sys.exit(2) if o in ("-c", "--compile"): i18ncompile = True if o in ("-m", "--merge"): i18nmerge = True if o in ("-l", "--collect"): i18ncollect = True load_config(configfile) if i18ncollect: print header("Transifex i18n collect") i18n_collect() if i18nmerge: print header("Transifex i18n merger") i18n_merge() if i18ncompile: print header("Transifex i18n compilation") i18n_compile() if __name__ == "__main__": sys.exit(main())