#!/usr/bin/env python # -*- 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 lang resources in the specified directory' print 'Options: -h, --help : usage' print ' -d , --directory : directory with lang 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') for langfile in glob.glob(os.path.join(directory, '*.en.lang')): langfiledir = langfile.replace('.en.lang', '') langfilename = langfiledir.rpartition('/')[2] # Create localization directories if needed podir = langfiledir + '/po' try: os.makedirs(podir) except OSError, e: if e.errno != errno.EEXIST: raise #open lang file text = open(langfile,"r").read() newtext = text.split('\n\n',1)[0] # Write POT file pot = polib.POFile() potcreationtime = strftime('%Y-%m-%d %H:%M%z') pot.metadata = { 'Project-Id-Version': langfile, 'Report-Msgid-Bugs-To': 'mageia-i18n@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=CHARSET', 'Content-Transfer-Encoding': '8bit', } # Parse contents and add them to POT messagetemplate='\n\n#\ .*?\n\;.*?\n.*?\n' mpattern=re.compile(messagetemplate,re.DOTALL) for mblock in mpattern.findall(text): mblock_stripped = mblock.strip('\n') message_comment, message_text = mblock.strip('\n').split('\n;',1) # Strip '# ' from comments message_comment = message_comment.lstrip('# ').replace('\n# ',' Comment: ') message_id, message_str = message_text.split('\n',1) # Strip ';' from msgid message_id = message_id.lstrip(';') potentry = polib.POEntry( msgctxt = message_comment, msgid = message_id.decode('utf-8'), msgstr = '', occurrences=[(langfile,'')] ) pot.append(potentry) potfilename = langfiledir + '/po/' + langfilename + '.pot' pot.save(potfilename) # Parse PO files 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) content = newtext + '\n\n' for entry in po.translated_entries(): content = content + ('# ' + entry.msgctxt + '\n;' + entry.msgid + '\n' + entry.msgstr).encode('utf-8') if entry.msgid == entry.msgstr: content = content + ' {ok}' content = content + '\n\n' translatedlang = open(langfile.replace('.en.lang','') + '.' + lang + '.lang', "w") translatedlang.write(content) translatedlang.close()