aboutsummaryrefslogtreecommitdiffstats
path: root/websites/langpo.py
diff options
context:
space:
mode:
authorYuri Chornoivan <yurchor@mageia.org>2012-09-09 14:00:09 +0000
committerYuri Chornoivan <yurchor@mageia.org>2012-09-09 14:00:09 +0000
commite47d828404b901010e5646eaa1d70f9a12676073 (patch)
tree700b415bade445577d2120721a3a72da1dc27e40 /websites/langpo.py
parent465a20a84e53b2fab8949e382ae86ee47134935a (diff)
downloadtools-e47d828404b901010e5646eaa1d70f9a12676073.tar
tools-e47d828404b901010e5646eaa1d70f9a12676073.tar.gz
tools-e47d828404b901010e5646eaa1d70f9a12676073.tar.bz2
tools-e47d828404b901010e5646eaa1d70f9a12676073.tar.xz
tools-e47d828404b901010e5646eaa1d70f9a12676073.zip
Add scripts for web pages lang templates conversion to POT/PO files and vice versa
Diffstat (limited to 'websites/langpo.py')
-rw-r--r--websites/langpo.py72
1 files changed, 72 insertions, 0 deletions
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 <EMAIL@ADDRESS>',
+ 'Language-Team': 'LANGUAGE <LL@li.org>',
+ '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()
+