#!/usr/bin/env python ''' To the 'tg-admin i18n collect' work appropriately we have to apply this patch http://trac.turbogears.org/ticket/1695 ''' import sys import subprocess import os import getopt import glob import shutil from transifex.util import header, load_config, available_languages, get_locale, get_modules try: # try the python 2.5 way import xml.etree.ElementTree as ET except ImportError: # then try the python 2.4 way import elementtree.ElementTree as ET locale_dir = 'po/' def usage(name): print '''Transifex i18n tool. Handle the tradutions of the application easily. 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 # Create a .kid with descriptions and summaries of all modules registered in the database, so it can be translatable modules = get_modules(); file = ET.Element("html") body = ET.SubElement(file, "body") for module in modules: elem = ET.SubElement(file, "span") elem.text = module.description elem = ET.SubElement(file, "span") elem.text = module.summary tree = ET.ElementTree(file) tree.write("transifex/templates/modules_temp.kid") subprocess.call(['/usr/bin/tg-admin','i18n','collect']) os.remove("transifex/templates/modules_temp.kid") if os.path.exists(locale_dir + "transifex.bak"): os.remove(locale_dir + "transifex.bak") def i18n_merge(): poFiles = filter(os.path.isfile, glob.glob('po/*.po')) # Merge all langs availables in LINGUAS for pofile in poFiles: lang = os.path.basename(pofile).rsplit('.', 1)[0] potFile = locale_dir + 'transifex' + '.pot' poFile = locale_dir + pofile + '.po' print '''Merging %(lang)s language with the POT file...''' % {'lang': lang} subprocess.call(['/usr/bin/msgmerge', pofile, potFile,'-o', pofile]) print '\n' def i18n_compile(): # Delete diretories langs that aren't enable in LINGUAS diretories=[f for f in os.listdir(locale_dir) if os.path.isdir(locale_dir + f)] for d in diretories: if d not in available_languages(): #os.rmdir(locale_dir + d) shutil.rmtree(locale_dir +d) # Compile all langs availables in LINGUAS for lang in available_languages(): dirname = locale_dir + '%s/LC_MESSAGES/' % lang if not os.path.isdir(dirname): os.makedirs(dirname) subprocess.call(['/usr/bin/tg-admin','i18n','add',lang]) os.remove(dirname + 'transifex.po') mofile = dirname + 'transifex.mo' pofile = locale_dir + lang + '.po' subprocess.call(['/usr/bin/msgfmt', pofile, '-o', mofile]) print '''Compiling %(lang)s language...\n''' % {'lang': lang} 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())