2 # -*- coding: utf-8 -*-
3 # Written by Martin v. Löwis <loewis@informatik.hu-berlin.de>
4 # Plural forms support added by alexander smishlajev <alex@tycobka.lv>
6 """Generate binary message catalog from textual translation description.
8 This program converts a textual Uniforum-style message catalog (.po file) into
9 a binary GNU catalog (.mo file). This is essentially the same function as the
10 GNU msgfmt program, however, it is a simpler implementation.
12 Usage: msgfmt.py [OPTIONS] filename.po
17 Specify the output file to write to. If omitted, output will go to a
18 file named filename.mo (based off the input file name).
22 Print this message and exit.
26 Display version information and exit.
42 # Just a hack to translate desktop file
43 # l10n: Name of program shown in desktop file
44 DESKTOP_NAME
= _('Wammu')
45 # l10n: Generic name of program shown in desktop file
46 DESKTOP_GENERIC_NAME
= _('Mobile Phone Manager')
47 # l10n: Comment about program shown in desktop file
48 DESKTOP_COMMENT
= _('Application for mobile phones - frontend for Gammu')
50 DESKTOP_TRANSLATIONS
= { }
52 def usage(code
, msg
=''):
53 print >> sys
.stderr
, __doc__
55 print >> sys
.stderr
, msg
60 def add(id, str, fuzzy
):
61 "Add a non-fuzzy translation to the dictionary."
64 global DESKTOP_GENERIC_NAME
65 global DESKTOP_COMMENT
66 global DESKTOP_TRANSLATIONS
67 if not fuzzy
and str and not str.startswith('\0'):
69 if id == DESKTOP_NAME
:
70 DESKTOP_TRANSLATIONS
['Name'] = str
71 elif id == DESKTOP_GENERIC_NAME
:
72 DESKTOP_TRANSLATIONS
['GenericName'] = str
73 elif id == DESKTOP_COMMENT
:
74 DESKTOP_TRANSLATIONS
['Comment'] = str
77 "Return the generated output."
79 keys
= MESSAGES
.keys()
80 # the keys are sorted in the .mo file
85 # For each string, we need size and file offset. Each string is NUL
86 # terminated; the NUL does not count into the size.
87 offsets
.append((len(ids
), len(id), len(strs
), len(MESSAGES
[id])))
89 strs
+= MESSAGES
[id] + '\0'
91 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
92 # the keys start right after the index tables.
94 keystart
= 7*4+16*len(keys
)
95 # and the values start after the keys
96 valuestart
= keystart
+ len(ids
)
99 # The string table first has the list of keys, then the list of values.
100 # Each entry has first the size of the string, then the file offset.
101 for o1
, l1
, o2
, l2
in offsets
:
102 koffsets
+= [l1
, o1
+keystart
]
103 voffsets
+= [l2
, o2
+valuestart
]
104 offsets
= koffsets
+ voffsets
105 output
= struct
.pack("Iiiiiii",
108 len(keys
), # # of entries
109 7*4, # start of key index
110 7*4+len(keys
)*8, # start of value index
111 0, 0) # size and offset of hash table
112 output
+= array
.array("i", offsets
).tostring()
119 def make(filename
, outfile
):
123 global DESKTOP_TRANSLATIONS
125 DESKTOP_TRANSLATIONS
= {}
127 # Compute .mo name from .po name and arguments
128 if filename
.endswith('.po'):
131 infile
= filename
+ '.po'
133 outfile
= os
.path
.splitext(infile
)[0] + '.mo'
136 lines
= open(infile
).readlines()
138 print >> sys
.stderr
, msg
148 # If we get a comment line after a msgstr, this is a new entry
149 if l
[0] == '#' and section
== STR
:
150 add(msgid
, msgstr
, fuzzy
)
153 # Record a fuzzy mark
154 if l
[:2] == '#,' and (l
.find('fuzzy') >= 0):
159 # Start of msgid_plural section, separate from singular form with \0
160 if l
.startswith('msgid_plural'):
163 # Now we are in a msgid section, output previous section
164 elif l
.startswith('msgid'):
166 add(msgid
, msgstr
, fuzzy
)
170 # Now we are in a msgstr section
171 elif l
.startswith('msgstr'):
174 # Check for plural forms
175 if l
.startswith('['):
176 # Separate plural forms with \0
177 if not l
.startswith('[0]'):
179 # Ignore the index - must come in sequence
180 l
= l
[l
.index(']') + 1:]
185 # XXX: Does this always follow Python escape semantics?
192 print >> sys
.stderr
, 'Syntax error on %s:%d' % (infile
, lno
), \
194 print >> sys
.stderr
, l
198 add(msgid
, msgstr
, fuzzy
)
204 open(outfile
,"wb").write(output
)
206 print >> sys
.stderr
, msg
212 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'hVo:',
213 ['help', 'version', 'output-file='])
214 except getopt
.error
, msg
:
219 for opt
, arg
in opts
:
220 if opt
in ('-h', '--help'):
222 elif opt
in ('-V', '--version'):
223 print >> sys
.stderr
, "msgfmt.py", __version__
225 elif opt
in ('-o', '--output-file'):
229 print >> sys
.stderr
, 'No input file given'
230 print >> sys
.stderr
, "Try `msgfmt --help' for more information."
233 for filename
in args
:
234 make(filename
, outfile
)
237 if __name__
== '__main__':