]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Tools/i18n/msgfmt.py
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.
40 def usage(code
, msg
=''):
41 print >> sys
.stderr
, __doc__
43 print >> sys
.stderr
, msg
48 def add(id, str, fuzzy
):
49 "Add a non-fuzzy translation to the dictionary."
57 "Return the generated output."
59 keys
= MESSAGES
.keys()
60 # the keys are sorted in the .mo file
65 # For each string, we need size and file offset. Each string is NUL
66 # terminated; the NUL does not count into the size.
67 offsets
.append((len(ids
), len(id), len(strs
), len(MESSAGES
[id])))
69 strs
+= MESSAGES
[id] + '\0'
71 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
72 # the keys start right after the index tables.
74 keystart
= 7*4+16*len(keys
)
75 # and the values start after the keys
76 valuestart
= keystart
+ len(ids
)
79 # The string table first has the list of keys, then the list of values.
80 # Each entry has first the size of the string, then the file offset.
81 for o1
, l1
, o2
, l2
in offsets
:
82 koffsets
+= [l1
, o1
+keystart
]
83 voffsets
+= [l2
, o2
+valuestart
]
84 offsets
= koffsets
+ voffsets
85 output
= struct
.pack("Iiiiiii",
88 len(keys
), # # of entries
89 7*4, # start of key index
90 7*4+len(keys
)*8, # start of value index
91 0, 0) # size and offset of hash table
92 output
+= array
.array("i", offsets
).tostring()
99 def make(filename
, outfile
):
103 # Compute .mo name from .po name and arguments
104 if filename
.endswith('.po'):
107 infile
= filename
+ '.po'
109 outfile
= os
.path
.splitext(infile
)[0] + '.mo'
112 lines
= open(infile
).readlines()
114 print >> sys
.stderr
, msg
124 # If we get a comment line after a msgstr, this is a new entry
125 if l
[0] == '#' and section
== STR
:
126 add(msgid
, msgstr
, fuzzy
)
129 # Record a fuzzy mark
130 if l
[:2] == '#,' and 'fuzzy' in l
:
135 # Now we are in a msgid section, output previous section
136 if l
.startswith('msgid') and not l
.startswith('msgid_plural'):
138 add(msgid
, msgstr
, fuzzy
)
143 # This is a message with plural forms
144 elif l
.startswith('msgid_plural'):
146 print >> sys
.stderr
, 'msgid_plural not preceeded by msgid on %s:%d' %\
150 msgid
+= '\0' # separator of singular and plural
152 # Now we are in a msgstr section
153 elif l
.startswith('msgstr'):
155 if l
.startswith('msgstr['):
157 print >> sys
.stderr
, 'plural without msgid_plural on %s:%d' %\
160 l
= l
.split(']', 1)[1]
162 msgstr
+= '\0' # Separator of the various plural forms
165 print >> sys
.stderr
, 'indexed msgstr required for plural on %s:%d' %\
173 # XXX: Does this always follow Python escape semantics?
180 print >> sys
.stderr
, 'Syntax error on %s:%d' % (infile
, lno
), \
182 print >> sys
.stderr
, l
186 add(msgid
, msgstr
, fuzzy
)
192 open(outfile
,"wb").write(output
)
194 print >> sys
.stderr
, msg
200 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'hVo:',
201 ['help', 'version', 'output-file='])
202 except getopt
.error
, msg
:
207 for opt
, arg
in opts
:
208 if opt
in ('-h', '--help'):
210 elif opt
in ('-V', '--version'):
211 print >> sys
.stderr
, "msgfmt.py", __version__
213 elif opt
in ('-o', '--output-file'):
217 print >> sys
.stderr
, 'No input file given'
218 print >> sys
.stderr
, "Try `msgfmt --help' for more information."
221 for filename
in args
:
222 make(filename
, outfile
)
225 if __name__
== '__main__':