From e47d828404b901010e5646eaa1d70f9a12676073 Mon Sep 17 00:00:00 2001 From: Yuri Chornoivan Date: Sun, 9 Sep 2012 14:00:09 +0000 Subject: Add scripts for web pages lang templates conversion to POT/PO files and vice versa --- websites/README | 51 ++++++++++++++++++++++++++ websites/langpo.py | 72 +++++++++++++++++++++++++++++++++++++ websites/translated_converter.py | 77 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 websites/README create mode 100644 websites/langpo.py create mode 100644 websites/translated_converter.py (limited to 'websites') diff --git a/websites/README b/websites/README new file mode 100644 index 00000000..b4d89a49 --- /dev/null +++ b/websites/README @@ -0,0 +1,51 @@ +For some reasons usual conversion scripts for .lang files from Translate Toolkit +do not work as expected. + +Nevertheless, the attached script may be useful for those who want to +organize sustainable translation process of web pages translation. + +Prerequisites: + +Installed python-polib package: + +# urpmi python-polib + +I langpo.py: + +Usage: + +1. Copy the script into the folder with English .lang files. +2. Run + python langpo.py +3. Script will create .pot's for all .lang's in the folder. Translate pot's and + rename the translations into .po +4. Run + python langpo.py + once more to create lang's for your language. + +If you ever need to update something (.lang's were updated or you found a typo, +etc.) just run + python langpo.py + +Limitations: +The script is useful only for Unicode translations. + +II translated_converter.py is a one-time converter for existing +translations. + +Run it as + +python translated_converter.py -l + +(Ex.: python translated_converter.py -l el) + +in a folder of translations. Then copy po's with their folders into /en folder +and run + +python langpo.py + + * Both of these files are free software. They come without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify them under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. diff --git a/websites/langpo.py b/websites/langpo.py new file mode 100644 index 00000000..703635b6 --- /dev/null +++ b/websites/langpo.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import errno, glob, polib, re, os +from time import strftime + +for langfile in glob.glob('*.en.lang'): + langfilename = langfile.replace('.en.lang', '') + # Create localization directories if needed + podir = langfilename + '/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' + 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('# ') + 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 = langfilename + '/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.rstrip('.en.lang') + '.' + lang + '.lang', "w") + translatedlang.write(content) + translatedlang.close() + diff --git a/websites/translated_converter.py b/websites/translated_converter.py new file mode 100644 index 00000000..14c17f32 --- /dev/null +++ b/websites/translated_converter.py @@ -0,0 +1,77 @@ +#!/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 ' generates po file for existing lang translations' + print 'Options: -h, --help : usage' + print ' -l lang_code, --language lang_code : code of the translation language' + sys.exit(2) +try: + opts, args = getopt.getopt(sys.argv[1:], "hl:", ["help", "language="]) +except getopt.GetoptError: + usage() # print help information and exit + +language='' +for o,a in opts: + if o in ("-h", "--help"): + usage() + if o in ("-l", "--language"): + lang_code=a + +if lang_code == '': + sys.exit('Empty language code') + +file_extension = '.' + lang_code + '.lang' +file_mask = '*' + file_extension +for langfile in glob.glob(file_mask): + langfilename = langfile.replace(file_extension, '') + # Create localization directories if needed + podir = langfilename + '/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 PO file + po = polib.POFile() + pocreationtime = strftime('%Y-%m-%d %H:%M%z') + po.metadata = { + 'Project-Id-Version': langfile, + 'Report-Msgid-Bugs-To': 'mageia-i18n@mageia.org', + 'POT-Creation-Date': pocreationtime, + 'PO-Revision-Date': pocreationtime, + 'Last-Translator': 'Duffy Duck ', + 'Language-Team': lang_code + ' ', + 'MIME-Version': '1.0', + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding': '8bit', + } + + # Parse contents and add them to POT + messagetemplate='\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('# ') + 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 = message_str.decode('utf-8'), + occurrences=[(langfile,'')] + ) + po.append(potentry) + pofilename = langfilename + '/po/' + lang_code + '.po' + po.save(pofilename) -- cgit v1.2.1