2
# -*- coding: utf-8 -*-
3
# Written by Martin v. Lowis <loewis@informatik.hu-berlin.de>
5
# This script is copied from Python/Tools/i18n/msgfmt.py
6
# It licensed under PSF license which compatible with GPL.
9
# * Convert encoding from iso-8859-1 to utf-8
10
# * Fix http://bugs.python.org/issue9741
12
"""Generate binary message catalog from textual translation description.
14
This program converts a textual Uniforum-style message catalog (.po file) into
15
a binary GNU catalog (.mo file). This is essentially the same function as the
16
GNU msgfmt program, however, it is a simpler implementation.
18
Usage: msgfmt.py [OPTIONS] filename.po
23
Specify the output file to write to. If omitted, output will go to a
24
file named filename.mo (based off the input file name).
28
Print this message and exit.
32
Display version information and exit.
46
def usage(code, msg=''):
47
print >> sys.stderr, __doc__
49
print >> sys.stderr, msg
53
def add(id, str, fuzzy):
54
"Add a non-fuzzy translation to the dictionary."
61
"Return the generated output."
63
keys = MESSAGES.keys()
64
# the keys are sorted in the .mo file
69
# For each string, we need size and file offset. Each string is NUL
70
# terminated; the NUL does not count into the size.
71
offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
73
strs += MESSAGES[id] + '\0'
75
# The header is 7 32-bit unsigned integers. We don't use hash tables, so
76
# the keys start right after the index tables.
78
keystart = 7*4+16*len(keys)
79
# and the values start after the keys
80
valuestart = keystart + len(ids)
83
# The string table first has the list of keys, then the list of values.
84
# Each entry has first the size of the string, then the file offset.
85
for o1, l1, o2, l2 in offsets:
86
koffsets += [l1, o1+keystart]
87
voffsets += [l2, o2+valuestart]
88
offsets = koffsets + voffsets
89
output = struct.pack("Iiiiiii",
92
len(keys), # # of entries
93
7*4, # start of key index
94
7*4+len(keys)*8, # start of value index
95
0, 0) # size and offset of hash table
96
output += array.array("i", offsets).tostring()
102
def make(filename, outfile):
103
# Fix http://bugs.python.org/issue9741
109
# Compute .mo name from .po name and arguments
110
if filename.endswith('.po'):
113
infile = filename + '.po'
115
outfile = os.path.splitext(infile)[0] + '.mo'
118
lines = open(infile).readlines()
120
print >> sys.stderr, msg
130
# If we get a comment line after a msgstr, this is a new entry
131
if l[0] == '#' and section == STR:
132
add(msgid, msgstr, fuzzy)
135
# Record a fuzzy mark
136
if l[:2] == '#,' and 'fuzzy' in l:
141
# Now we are in a msgid section, output previous section
142
if l.startswith('msgid'):
144
add(msgid, msgstr, fuzzy)
148
# Now we are in a msgstr section
149
elif l.startswith('msgstr'):
156
# XXX: Does this always follow Python escape semantics?
163
print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
165
print >> sys.stderr, l
169
add(msgid, msgstr, fuzzy)
175
open(outfile,"wb").write(output)
177
print >> sys.stderr, msg
182
opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
183
['help', 'version', 'output-file='])
184
except getopt.error, msg:
189
for opt, arg in opts:
190
if opt in ('-h', '--help'):
192
elif opt in ('-V', '--version'):
193
print >> sys.stderr, "msgfmt.py", __version__
195
elif opt in ('-o', '--output-file'):
199
print >> sys.stderr, 'No input file given'
200
print >> sys.stderr, "Try `msgfmt --help' for more information."
203
for filename in args:
204
make(filename, outfile)
207
if __name__ == '__main__':