aboutsummaryrefslogtreecommitdiffstats
path: root/docs/stylesheets/xslpo.py
diff options
context:
space:
mode:
authorYuri Chornoivan <yurchor@ukr.net>2015-11-03 16:26:08 +0200
committerYuri Chornoivan <yurchor@ukr.net>2015-11-03 16:26:08 +0200
commit6f8630a70dd5a4652cc897c75e5692c28f1b7dd4 (patch)
tree7cf0a6cb3c787769dd2f3fb0e7d5672f038080a9 /docs/stylesheets/xslpo.py
parent812bc098c3325d87a5638238ae9d802b44d20eb3 (diff)
downloadtools-6f8630a70dd5a4652cc897c75e5692c28f1b7dd4.tar
tools-6f8630a70dd5a4652cc897c75e5692c28f1b7dd4.tar.gz
tools-6f8630a70dd5a4652cc897c75e5692c28f1b7dd4.tar.bz2
tools-6f8630a70dd5a4652cc897c75e5692c28f1b7dd4.tar.xz
tools-6f8630a70dd5a4652cc897c75e5692c28f1b7dd4.zip
Add files to translate messages from stylesheets
Diffstat (limited to 'docs/stylesheets/xslpo.py')
-rw-r--r--docs/stylesheets/xslpo.py129
1 files changed, 129 insertions, 0 deletions
diff --git a/docs/stylesheets/xslpo.py b/docs/stylesheets/xslpo.py
new file mode 100644
index 00000000..7ebd5046
--- /dev/null
+++ b/docs/stylesheets/xslpo.py
@@ -0,0 +1,129 @@
+#!/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 xsl resources in the specified directory'
+ print 'Options: -h, --help : usage'
+ print ' -d <directory>, --directory <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='<l:gentext key=\"(.*?)\"\/>'
+mpattern=re.compile(messagetemplate,re.DOTALL)
+transblocktemplate='<l:i18n xmlns:l="http:\/\/docbook.sourceforge.net\/xmlns\/l10n\/1.0\">(.*?)<\/l:i18n>'
+tbpattern=re.compile(transblocktemplate,re.DOTALL)
+blocktemplate='<l:i18n xmlns:l=\"http://docbook.sourceforge.net/xmlns/l10n/1.0\">\n</l:i18n>'
+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 <EMAIL@ADDRESS>',
+ 'Language-Team': 'LANGUAGE <LL@li.org>',
+ '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, 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.decode('utf-8'),
+ 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<l:l10n language=\"%s\">\n' %lang
+ for entry in po:
+ if entry.translated():
+ translatedtext += u'\t\t<l:gentext key=\"%s\" text=\"%s\"/>\n' %(entry.msgctxt, entry.msgstr)
+ else:
+ translatedtext += '\t\t<l:gentext key=\"%s\" text=\"%s\"/>\n' %(entry.msgctxt, messages[entry.msgctxt])
+ translatedtext += '\t</l:l10n>\n</l:i18n>'
+ text = text.replace('</l:i18n>', translatedtext)
+
+ # write file
+ deskfile.write(text.encode('utf-8'))
+ deskfile.close()
+