-
Notifications
You must be signed in to change notification settings - Fork 2
/
transwxl2po.py
executable file
·160 lines (130 loc) · 4.63 KB
/
transwxl2po.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# coding:utf-8
"""transwxl2po.py: Transform a main language wxl file and a translated wxl file into a .po translation file"""
__author__ = "Sébastien Blaisot ([email protected])"
__copyright__ = "Copyright (C) 2016-2017 Sébastien Blaisot"
__license__ = "GPL 3.0"
__version__ = "0.2-pre"
__status__ = "Development"
import getopt
import sys
import textwrap
import os.path
from xml.dom import minidom
import polib
def version():
print os.path.basename(__file__) + " version " + __version__ + "\n"
def help():
print textwrap.dedent("""
Usage: %s [OPTION]... WXL_SOURCE_FILE WXL_TRANSLATED_FILE PO_DEST_FILE
Transform the file WXL_SOURCE_FILE in wxl format into a po file PO_DEST_FILE
containing the translations from WXL_TRANSLATED_FILE
Example: %s -l LangId en-us.wxl fr-fr.wxl fr-fr.po
Options:
-h, --help: print this help message and exit
-V, --version print version information and exit
-f, --force don't ask before overwriting destination file
-l, --langid=LANGID ignore string with Id LANGID containing the LCID
""" % (os.path.basename(__file__), os.path.basename(__file__)))
def usage():
print textwrap.dedent("""\
Usage: %s [OPTION]... WXL_SOURCE_FILE WXL_TRANSLATED_FILE PO_DEST_FILE
Try '%s --help' for more information.
""" % (os.path.basename(__file__), os.path.basename(__file__)))
# Main
langid = ""
force = False
try:
opts, args = getopt.getopt(sys.argv[1:], "hVfl:", ["help", "version", "force", "langid="])
except getopt.GetoptError as err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
output = None
verbose = False
for o, a in opts:
if o in ("-V", "--version"):
version()
sys.exit()
elif o in ("-h", "--help"):
help()
sys.exit()
elif o in ("-f", "--force"):
force = True
elif o in ("-l", "--langid"):
langid = a
else:
assert False, "unhandled option"
if len(args) < 3:
print "Missing filename parameters"
usage()
sys.exit(1)
sourcefile = args[0]
transfile = args[1]
destfile = args[2]
if not os.path.exists(sourcefile):
print "Source file " + sourcefile + " does not exist. Please provide a valid wxl file."
sys.exit(1)
if not os.path.exists(transfile):
print "Translated file " + transfile + " does not exist. Please provide a valid wxl file."
sys.exit(1)
if os.path.exists(destfile) and not force:
sys.stdout.write("Destination file " + destfile + " already exists. Overwrite ? [y/N] ")
choice = raw_input().lower()
if choice not in ['yes','y', 'ye']:
print "Aborting"
sys.exit(1)
transdoc = minidom.parse(transfile)
transwixloc = transdoc.getElementsByTagName("WixLocalization")[0]
transculture = transwixloc.getAttribute("Culture")
transculture = transculture[:transculture.index('-')] + '_' + transculture[(transculture.index('-') + 1):].upper()
transroot = transdoc.documentElement
transnodes = transroot.childNodes
translatedStrings = {}
for node in transnodes:
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == "String":
stringId = node.getAttribute("Id")
if stringId == langid:
comment = ""
continue
stringContent = node.firstChild.data
translatedStrings[stringId] = stringContent
doc = minidom.parse(sourcefile)
wixloc = doc.getElementsByTagName("WixLocalization")[0]
culture = wixloc.getAttribute("Culture")
codepage = wixloc.getAttribute("Codepage")
po = polib.POFile(wrapwidth=0)
po.metadata = {
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=utf-8',
'Content-Transfer-Encoding': '8bit',
'Language': transculture
}
root = doc.documentElement
nodes = root.childNodes
comment = ""
for node in nodes:
if node.nodeType == node.COMMENT_NODE:
comment = node.data
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == "String":
stringId = node.getAttribute("Id")
if stringId == langid:
continue
stringContent = node.firstChild.data
if stringId in translatedStrings:
translation = translatedStrings[stringId]
else:
translation = stringContent;
entry = polib.POEntry(
comment = comment,
msgctxt = stringId,
msgid = stringContent,
msgstr = translation
)
po.append(entry)
if comment != "":
comment = ""
po.save(destfile)