2 # -*- coding: iso-8859-1 -*-
4 # This file is part of Gnomolicious and is distributed under the Python
5 # Software License (http://www.python.org/2.3/license.html).
7 # Permission to use, copy, modify, and distribute this software and its
8 # documentation for any purpose and without fee is hereby granted,
9 # provided that the above copyright notice appear in all copies and that
10 # both that copyright notice and this permission notice appear in
11 # supporting documentation, and that the name of Stichting Mathematisch
12 # Centrum or CWI not be used in advertising or publicity pertaining to
13 # distribution of the software without specific, written prior
16 # Written by Martin v. Löwis <loewis@informatik.hu-berlin.de>
18 """Generate binary message catalog from textual translation description.
20 This program converts a textual Uniforum-style message catalog (.po file) into
21 a binary GNU catalog (.mo file). This is essentially the same function as the
22 GNU msgfmt program, however, it is a simpler implementation.
24 Usage: msgfmt.py [OPTIONS] filename.po
29 Specify the output file to write to. If omitted, output will go to a
30 file named filename.mo (based off the input file name).
34 Print this message and exit.
38 Display version information and exit.
49 def usage(code
, msg
=''):
50 print >> sys
.stderr
, __doc__
52 print >> sys
.stderr
, msg
55 def add(id, str, fuzzy
):
56 "Add a non-fuzzy translation to the dictionary."
62 "Return the generated output."
64 keys
= MESSAGES
.keys()
65 # the keys are sorted in the .mo file
70 # For each string, we need size and file offset. Each string is NUL
71 # terminated; the NUL does not count into the size.
72 offsets
.append((len(ids
), len(id), len(strs
), len(MESSAGES
[id])))
74 strs
+= MESSAGES
[id] + '\0'
76 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
77 # the keys start right after the index tables.
79 keystart
= 7*4+16*len(keys
)
80 # and the values start after the keys
81 valuestart
= keystart
+ len(ids
)
84 # The string table first has the list of keys, then the list of values.
85 # Each entry has first the size of the string, then the file offset.
86 for o1
, l1
, o2
, l2
in offsets
:
87 koffsets
+= [l1
, o1
+keystart
]
88 voffsets
+= [l2
, o2
+valuestart
]
89 offsets
= koffsets
+ voffsets
90 output
= struct
.pack("Iiiiiii",
93 len(keys
), # # of entries
94 7*4, # start of key index
95 7*4+len(keys
)*8, # start of value index
96 0, 0) # size and offset of hash table
97 output
+= array
.array("i", offsets
).tostring()
104 def make(filename
, outfile
):
110 # Compute .mo name from .po name and arguments
111 if filename
.endswith('.po'):
114 infile
= filename
+ '.po'
116 outfile
= os
.path
.splitext(infile
)[0] + '.mo'
119 lines
= open(infile
).readlines()
121 print >> sys
.stderr
, msg
131 # If we get a comment line after a msgstr, this is a new entry
132 if l
[0] == '#' and section
== STR
:
133 add(msgid
, msgstr
, fuzzy
)
136 # Record a fuzzy mark
137 if l
[:2] == '#,' and l
.count('fuzzy'):
142 # Now we are in a msgid section, output previous section
143 if l
.startswith('msgid'):
145 add(msgid
, msgstr
, fuzzy
)
149 # Now we are in a msgstr section
150 elif l
.startswith('msgstr'):
157 # XXX: Does this always follow Python escape semantics?
164 print >> sys
.stderr
, 'Syntax error on %s:%d' % (infile
, lno
), \
166 print >> sys
.stderr
, l
170 add(msgid
, msgstr
, fuzzy
)
176 open(outfile
,"wb").write(output
)
178 print >> sys
.stderr
, msg
184 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'hVo:',
185 ['help', 'version', 'output-file='])
186 except getopt
.error
, msg
:
191 for opt
, arg
in opts
:
192 if opt
in ('-h', '--help'):
194 elif opt
in ('-V', '--version'):
195 print >> sys
.stderr
, "msgfmt.py", __version__
197 elif opt
in ('-o', '--output-file'):
201 print >> sys
.stderr
, 'No input file given'
202 print >> sys
.stderr
, "Try `msgfmt --help' for more information."
205 for filename
in args
:
206 make(filename
, outfile
)
209 if __name__
== '__main__':