1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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.lower()
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()
|