2 # -*- coding: iso-8859-1 -*-
3 # Written by Martin v. Löwis <loewis@informatik.hu-berlin.de>
5 """Generate binary message catalog from textual translation description.
7 This program converts a textual Uniforum-style message catalog (.po file) into
8 a binary GNU catalog (.mo file). This is essentially the same function as the
9 GNU msgfmt program, however, it is a simpler implementation.
11 Usage: msgfmt.py [OPTIONS] filename.po
16 Specify the output file to write to. If omitted, output will go to a
17 file named filename.mo (based off the input file name).
21 Print this message and exit.
25 Display version information and exit.
36 def usage(code
, msg
=''):
37 print >> sys
.stderr
, __doc__
39 print >> sys
.stderr
, msg
42 def add(id, str, fuzzy
):
43 "Add a non-fuzzy translation to the dictionary."
49 "Return the generated output."
51 keys
= MESSAGES
.keys()
52 # the keys are sorted in the .mo file
57 # For each string, we need size and file offset. Each string is NUL
58 # terminated; the NUL does not count into the size.
59 offsets
.append((len(ids
), len(id), len(strs
), len(MESSAGES
[id])))
61 strs
+= MESSAGES
[id] + '\0'
63 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
64 # the keys start right after the index tables.
66 keystart
= 7*4+16*len(keys
)
67 # and the values start after the keys
68 valuestart
= keystart
+ len(ids
)
71 # The string table first has the list of keys, then the list of values.
72 # Each entry has first the size of the string, then the file offset.
73 for o1
, l1
, o2
, l2
in offsets
:
74 koffsets
+= [l1
, o1
+keystart
]
75 voffsets
+= [l2
, o2
+valuestart
]
76 offsets
= koffsets
+ voffsets
77 output
= struct
.pack("Iiiiiii",
80 len(keys
), # # of entries
81 7*4, # start of key index
82 7*4+len(keys
)*8, # start of value index
83 0, 0) # size and offset of hash table
84 output
+= array
.array("i", offsets
).tostring()
91 def make(filename
, outfile
):
97 # Compute .mo name from .po name and arguments
98 if filename
.endswith('.po'):
101 infile
= filename
+ '.po'
103 outfile
= os
.path
.splitext(infile
)[0] + '.mo'
106 lines
= open(infile
).readlines()
108 print >> sys
.stderr
, msg
118 # If we get a comment line after a msgstr, this is a new entry
119 if l
[0] == '#' and section
== STR
:
120 add(msgid
, msgstr
, fuzzy
)
123 # Record a fuzzy mark
124 if l
[:2] == '#,' and l
.count('fuzzy'):
129 # Now we are in a msgid section, output previous section
130 if l
.startswith('msgid'):
132 add(msgid
, msgstr
, fuzzy
)
136 # Now we are in a msgstr section
137 elif l
.startswith('msgstr'):
144 # XXX: Does this always follow Python escape semantics?
151 print >> sys
.stderr
, 'Syntax error on %s:%d' % (infile
, lno
), \
153 print >> sys
.stderr
, l
157 add(msgid
, msgstr
, fuzzy
)
163 open(outfile
,"wb").write(output
)
165 print >> sys
.stderr
, msg
171 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'hVo:',
172 ['help', 'version', 'output-file='])
173 except getopt
.error
, msg
:
178 for opt
, arg
in opts
:
179 if opt
in ('-h', '--help'):
181 elif opt
in ('-V', '--version'):
182 print >> sys
.stderr
, "msgfmt.py", __version__
184 elif opt
in ('-o', '--output-file'):
188 print >> sys
.stderr
, 'No input file given'
189 print >> sys
.stderr
, "Try `msgfmt --help' for more information."
192 for filename
in args
:
193 make(filename
, outfile
)
196 if __name__
== '__main__':