#!/usr/bin/env python3 # -*- coding: utf-8 -*- import errno, glob, polib, re, os, getopt, sys from time import strftime def usage(): print('\nUsage: python %s [OPTION]' %os.path.basename(sys.argv[0])) print(' generate pot catalogs and updates po files for xsl resources in the specified directory') print('Options: -h, --help : usage') print(' -d , --directory : directory with xsl files') sys.exit(2) try: opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory="]) except getopt.GetoptError: usage() # print help information and exit directory='.' for o,a in opts: if o in ("-h", "--help"): usage() if o in ("-d", "--directory"): directory=a directory = directory.rstrip('/') if (directory != '') and (os.path.isdir(directory) == False): sys.exit('Specified directory does not exist') # Find all XSL files files = [] for rootdir, dirnames, filenames in os.walk(directory): files.extend(glob.glob(rootdir + "/*.xsl")) # Define Templates and po directory name translationtemplate='l:l10n\ (.*?)<\/l:l10n' tpattern=re.compile(translationtemplate,re.DOTALL) entemplate='language=\"en\">\n(.*?)<\/l:l10n' enpattern=re.compile(entemplate,re.DOTALL) langtemplate='language=\"(.*?)\"' lpattern=re.compile(langtemplate,re.DOTALL) messagetemplate='' mpattern=re.compile(messagetemplate,re.DOTALL) transblocktemplate='(.*?)<\/l:i18n>' tbpattern=re.compile(transblocktemplate,re.DOTALL) blocktemplate='\n' podir = 'po' # Write POT file pot = polib.POFile('',check_for_duplicates=True) potcreationtime = strftime('%Y-%m-%d %H:%M%z') pot.metadata = { 'Project-Id-Version': 'Mageia XSL files translation', 'Report-Msgid-Bugs-To': 'i18n-discuss@ml.mageia.org', 'POT-Creation-Date': potcreationtime, 'PO-Revision-Date': 'YEAR-MO-DA HO:MI+ZONE', 'Last-Translator': 'FULL NAME ', 'Language-Team': 'LANGUAGE ', 'MIME-Version': '1.0', 'Content-Type': 'text/plain; charset=UTF-8', 'Content-Transfer-Encoding': '8bit', } for langfile in files: langfiledir = langfile.replace('.xsl', '') langfilename = langfiledir.rpartition('/')[2] # Create localization directories if needed try: os.makedirs(podir) except OSError as e: if e.errno != errno.EEXIST: raise #open xsl file text = open(langfile,"r").read() # Parse contents and add them to POT messages = {} for enblock in enpattern.findall(text): for enmessage in mpattern.findall(enblock): msgkey, msg_id = enmessage.split('\" text=\"') messages[msgkey] = msg_id potentry = polib.POEntry( msgctxt = msgkey, msgid = msg_id, msgstr = '', occurrences=[(langfile,'')] ) if msg_id != '': try: pot.append(potentry) except ValueError: print('') # Should be some warning, ignore now pot.save('po/doc_xsl.pot') # Merge translations for pofile in glob.glob(podir + '/*.po'): lang = pofile[:-3].rsplit('/',1)[1] pofilename = pofile po = polib.pofile(pofilename) po.merge(pot) po.save(pofilename) for langfile in files: #open xsl file deskfile = open(langfile,"r") text = deskfile.read() deskfile.close() deskfile = open(langfile,"w") for transblock in tbpattern.findall(text): text = text.replace(transblock, '\n') # Parse PO files for pofile in sorted(glob.glob(podir + '/*.po'), reverse = False): lang = pofile[:-3].rsplit('/',1)[1] pofilename = pofile po = polib.pofile(pofilename) translatedtext=u'\t\n' %lang.lower() for entry in po: if entry.translated(): translatedtext += u'\t\t\n' %(entry.msgctxt, entry.msgstr) else: translatedtext += '\t\t\n' %(entry.msgctxt, messages[entry.msgctxt]) translatedtext += '\t\n' text = text.replace('', translatedtext) # write file deskfile.write(text) deskfile.close()