1 # -*- coding: utf-8 -*-
2 # Written by Martin v. Lwis <loewis@informatik.hu-berlin.de>
3 # Plural forms support added by alexander smishlajev <alex@tycobka.lv>
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.
39 def usage (ecode, msg=''):
41 Print usage and msg and exit with given code.
43 print >> sys.stderr, __doc__
45 print >> sys.stderr, msg
49 def add (msgid, transtr, fuzzy):
51 Add a non-fuzzy translation to the dictionary.
54 if not fuzzy and transtr and not transtr.startswith('\0'):
55 MESSAGES[msgid] = transtr
60 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):
108 # Compute .mo name from .po name and arguments
109 if filename.endswith('.po'):
112 infile = filename + '.po'
114 outfile = os.path.splitext(infile)[0] + '.mo'
117 lines = open(infile).readlines()
119 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 (l.find('fuzzy') >= 0):
141 # Start of msgid_plural section, separate from singular form with \0
142 if l.startswith('msgid_plural'):
145 # Now we are in a msgid section, output previous section
146 elif l.startswith('msgid'):
148 add(msgid, msgstr, fuzzy)
152 # Now we are in a msgstr section
153 elif l.startswith('msgstr'):
156 # Check for plural forms
157 if l.startswith('['):
158 # Separate plural forms with \0
159 if not l.startswith('[0]'):
161 # Ignore the index - must come in sequence
162 l = l[l.index(']') + 1:]
167 # XXX: Does this always follow Python escape semantics?
174 print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
176 print >> sys.stderr, l
180 add(msgid, msgstr, fuzzy)
186 open(outfile,"wb").write(output)
188 print >> sys.stderr, msg
193 opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
194 ['help', 'version', 'output-file='])
195 except getopt.error, msg:
200 for opt, arg in opts:
201 if opt in ('-h', '--help'):
203 elif opt in ('-V', '--version'):
204 print >> sys.stderr, "msgfmt.py", __version__
206 elif opt in ('-o', '--output-file'):
210 print >> sys.stderr, 'No input file given'
211 print >> sys.stderr, "Try `msgfmt --help' for more information."
214 for filename in args:
215 make(filename, outfile)
218 if __name__ == '__main__':