Mercurial > babel > old > mirror
annotate babel/messages/mofile.py @ 176:b29bb8629a68
Extended the doctest to include tests for the fix on [176].
author | palgarvio |
---|---|
date | Tue, 26 Jun 2007 09:13:08 +0000 |
parents | c223445fdb82 |
children | bf9579c4b0ee |
rev | line source |
---|---|
162 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 """Writing of files in the ``gettext`` MO (machine object) format. | |
15 | |
16 :see: `The Format of MO Files | |
17 <http://www.gnu.org/software/gettext/manual/gettext.html#MO-Files>`_ | |
18 """ | |
19 | |
20 import array | |
21 import struct | |
22 | |
163 | 23 __all__ = ['write_mo'] |
24 __docformat__ = 'restructuredtext en' | |
25 | |
162 | 26 def write_mo(fileobj, catalog, use_fuzzy=False): |
27 """Write a catalog to the specified file-like object using the GNU MO file | |
28 format. | |
29 | |
30 >>> from babel.messages import Catalog | |
31 >>> from gettext import GNUTranslations | |
32 >>> from StringIO import StringIO | |
33 | |
34 >>> catalog = Catalog(locale='en_US') | |
35 >>> catalog.add('foo', 'Voh') | |
36 >>> catalog.add((u'bar', u'baz'), (u'Bahr', u'Batz')) | |
37 >>> catalog.add('fuz', 'Futz', flags=['fuzzy']) | |
174 | 38 >>> catalog.add('Fizz', '') |
176
b29bb8629a68
Extended the doctest to include tests for the fix on [176].
palgarvio
parents:
175
diff
changeset
|
39 >>> catalog.add(('Fuzz', 'Fuzzes'), ('', '')) |
162 | 40 >>> buf = StringIO() |
41 | |
42 >>> write_mo(buf, catalog) | |
43 >>> buf.seek(0) | |
44 >>> translations = GNUTranslations(fp=buf) | |
45 >>> translations.ugettext('foo') | |
46 u'Voh' | |
47 >>> translations.ungettext('bar', 'baz', 1) | |
48 u'Bahr' | |
49 >>> translations.ungettext('bar', 'baz', 2) | |
50 u'Batz' | |
51 >>> translations.ugettext('fuz') | |
52 u'fuz' | |
174 | 53 >>> translations.ugettext('Fizz') |
54 u'Fizz' | |
176
b29bb8629a68
Extended the doctest to include tests for the fix on [176].
palgarvio
parents:
175
diff
changeset
|
55 >>> translations.ugettext('Fuzz') |
b29bb8629a68
Extended the doctest to include tests for the fix on [176].
palgarvio
parents:
175
diff
changeset
|
56 u'Fuzz' |
b29bb8629a68
Extended the doctest to include tests for the fix on [176].
palgarvio
parents:
175
diff
changeset
|
57 >>> translations.ugettext('Fuzzes') |
b29bb8629a68
Extended the doctest to include tests for the fix on [176].
palgarvio
parents:
175
diff
changeset
|
58 u'Fuzzes' |
162 | 59 |
60 :param fileobj: the file-like object to write to | |
61 :param catalog: the `Catalog` instance | |
62 :param use_fuzzy: whether translations marked as "fuzzy" should be included | |
63 in the output | |
64 """ | |
65 messages = list(catalog) | |
66 if not use_fuzzy: | |
67 messages[1:] = [m for m in messages[1:] if not m.fuzzy] | |
68 messages.sort(lambda x,y: cmp(x.id, y.id)) | |
69 | |
70 ids = strs = '' | |
71 offsets = [] | |
72 | |
73 for message in messages: | |
74 # For each string, we need size and file offset. Each string is NUL | |
75 # terminated; the NUL does not count into the size. | |
76 if message.pluralizable: | |
77 msgid = '\x00'.join([ | |
78 msgid.encode(catalog.charset) for msgid in message.id | |
79 ]) | |
175
c223445fdb82
Forgot to fix the pluralizable messages, regarding #28.
palgarvio
parents:
174
diff
changeset
|
80 msgstrs = [] |
c223445fdb82
Forgot to fix the pluralizable messages, regarding #28.
palgarvio
parents:
174
diff
changeset
|
81 for idx, string in enumerate(message.string): |
c223445fdb82
Forgot to fix the pluralizable messages, regarding #28.
palgarvio
parents:
174
diff
changeset
|
82 if not string: |
c223445fdb82
Forgot to fix the pluralizable messages, regarding #28.
palgarvio
parents:
174
diff
changeset
|
83 msgstrs.append(message.id[idx]) |
c223445fdb82
Forgot to fix the pluralizable messages, regarding #28.
palgarvio
parents:
174
diff
changeset
|
84 else: |
c223445fdb82
Forgot to fix the pluralizable messages, regarding #28.
palgarvio
parents:
174
diff
changeset
|
85 msgstrs.append(string) |
162 | 86 msgstr = '\x00'.join([ |
175
c223445fdb82
Forgot to fix the pluralizable messages, regarding #28.
palgarvio
parents:
174
diff
changeset
|
87 msgstr.encode(catalog.charset) for msgstr in msgstrs |
162 | 88 ]) |
89 else: | |
90 msgid = message.id.encode(catalog.charset) | |
174 | 91 if not message.string: |
92 msgstr = message.id.encode(catalog.charset) | |
93 else: | |
94 msgstr = message.string.encode(catalog.charset) | |
162 | 95 offsets.append((len(ids), len(msgid), len(strs), len(msgstr))) |
96 ids += msgid + '\x00' | |
97 strs += msgstr + '\x00' | |
98 | |
99 # The header is 7 32-bit unsigned integers. We don't use hash tables, so | |
100 # the keys start right after the index tables. | |
101 keystart = 7 * 4 + 16 * len(messages) | |
102 valuestart = keystart + len(ids) | |
103 | |
104 # The string table first has the list of keys, then the list of values. | |
105 # Each entry has first the size of the string, then the file offset. | |
106 koffsets = [] | |
107 voffsets = [] | |
108 for o1, l1, o2, l2 in offsets: | |
109 koffsets += [l1, o1 + keystart] | |
110 voffsets += [l2, o2 + valuestart] | |
111 offsets = koffsets + voffsets | |
112 | |
113 fileobj.write(struct.pack('Iiiiiii', | |
114 0x950412deL, # magic | |
115 0, # version | |
116 len(messages), # number of entries | |
117 7 * 4, # start of key index | |
118 7 * 4 + len(messages) * 8, # start of value index | |
119 0, 0 # size and offset of hash table | |
120 ) + array.array("i", offsets).tostring() + ids + strs) |