-
Notifications
You must be signed in to change notification settings - Fork 2
/
po2wxl.py
executable file
·176 lines (148 loc) · 6.04 KB
/
po2wxl.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# coding:utf-8
"""po2wxl.py: Transform a .po translation file into a wxl localization 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.sax.saxutils import escape
import polib
from lcid import LCIDs
def version():
print os.path.basename(__file__) + " version " + __version__ + "\n"
def help():
print textwrap.dedent("""
Usage: %s [OPTION]... PO_SOURCE_FILE WXL_DEST_FILE
Transform the file PO_SOURCE_FILE in po format into a wxl file WXL_DEST_FILE
Example: %s -l LangId en-us.po en-us.wxl
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 automatically determine LCID based on language and
add a string with id LANGID containing the LCID
-L, --LCID=LCID used with -l, use provided LCID instead of trying
to guess it
-C, --codepage=CP use CP as codepage instead of trying to guess it
-p, --percentlimit=LIMIT do not translate po files which translation percent
is below LIMIT. 60% by default
""" % (os.path.basename(__file__), os.path.basename(__file__)))
def usage():
print textwrap.dedent("""\
Usage: %s [OPTION]... PO_SOURCE_FILE WXL_DEST_FILE
Try '%s --help' for more information.
""" % (os.path.basename(__file__), os.path.basename(__file__)))
# Main
langid = ""
codepage = ""
LCID=""
translationPercentLimit = 60
force = False
try:
opts, args = getopt.getopt(sys.argv[1:], "hVfl:L:C:p:", ["help", "version", "force", "langid=", "LCID=", "codepage=", "percentlimit="])
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.encode("utf-8")
elif o in ("-L", "--LCID"):
LCID = a.encode("utf-8")
elif o in ("-C", "--codepage"):
codepage = a.encode("utf-8")
elif o in ("-p", "--percentlimit"):
translationPercentLimit = int(a)
else:
assert False, "unhandled option"
if len(args) < 2:
print "Missing filename parameters"
usage()
sys.exit(1)
sourcefile = args[0]
destfile = args[1]
if not os.path.exists(sourcefile):
print "Source file " + sourcefile + " 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)
po = polib.pofile(sourcefile)
if po.percent_translated() <= translationPercentLimit:
print "Skipping " + sourcefile + ": translated at " + str(po.percent_translated()) + "%, below the " + str(translationPercentLimit) + "% limit\n"
sys.exit(0)
metadata = po.ordered_metadata()
language = [value for name, value in metadata if name == "Language"]
culture = language[0].lower().replace('_','-')
cultureShort = culture[:culture.index('-')]
if codepage == "":
if culture in LCIDs.keys():
codepage = LCIDs[culture]['codepage']
elif cultureShort in LCIDs.keys():
codepage = LCIDs[cultureShort]['codepage']
else:
print "Unable to guess codepage based on language " + culture
print "Please provide codepage with option -C"
print "Try 'po2wxl.py --help' for more information."
sys.exit(1)
if not codepage:
codepage = "65001" # UTF-8 fallback
if LCID != "":
langIdAuto = LCID
else:
if culture in LCIDs.keys():
langIdAuto = LCIDs[culture]['LCID']
elif cultureShort in LCIDs.keys():
langIdAuto = LCIDs[cultureShort]['LCID']
else:
print "Unable to guess LCID based on language " + culture
print "Please provide LCID with option -L"
print "Try 'po2wxl.py --help' for more information."
sys.exit(1);
f = open(destfile,'w')
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f.write("<WixLocalization Culture=\"" + culture + "\" Codepage=\"" + str(codepage) + "\"\n")
f.write(" xmlns=\"http://schemas.microsoft.com/wix/2006/localization\">\n")
f.write("\n")
f.write(" <!-- ..................................................... -->\n")
f.write(" <!-- This wxl file has been auto generated from a po file -->\n")
f.write(" <!-- using https://github.com/sblaisot/wxl-po-tools -->\n")
f.write(" <!-- Source File: " + sourcefile.ljust(40) + " -->\n")
f.write(" <!-- ..................................................... -->\n")
f.write("\n")
if langid:
f.write(" <!-- This contains the LangID and should be translated to reflect the correct LangID. -->\n")
f.write(" <!-- Supported language and codepage codes can be found here: https://msdn.microsoft.com/en-us/goglobal/bb964664.aspx -->\n")
f.write(" <String Id=\"" + langid + "\">" + str(langIdAuto) + "</String>\n")
f.write("\n")
for entry in po:
if entry.comment != "":
f.write("\n")
f.write(" <!--" + entry.comment.replace('\n', ' -->\n <!--') + " -->\n")
if entry.msgstr != "":
translation = escape(entry.msgstr)
else:
translation = escape(entry.msgid)
translation = " ".join(translation.split("\n")).replace('\r', '').encode("utf-8")
f.write(" <String Id=\"" + entry.msgctxt.encode("utf-8") + "\">" + translation + "</String>\n")
f.write("</WixLocalization>\n")
f.close