Skip to content

Commit d2cf189

Browse files
authored
Merge pull request #564 from cedk/skip-empty-msgid
Skip empty message when writing mo file
2 parents 4222e1a + 85f6587 commit d2cf189

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

babel/messages/mofile.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ def write_mo(fileobj, catalog, use_fuzzy=False):
153153
in the output
154154
"""
155155
messages = list(catalog)
156-
if not use_fuzzy:
157-
messages[1:] = [m for m in messages[1:] if not m.fuzzy]
156+
messages[1:] = [m for m in messages[1:]
157+
if m.string and (use_fuzzy or not m.fuzzy)]
158158
messages.sort()
159159

160160
ids = strs = b''
@@ -178,10 +178,7 @@ def write_mo(fileobj, catalog, use_fuzzy=False):
178178
])
179179
else:
180180
msgid = message.id.encode(catalog.charset)
181-
if not message.string:
182-
msgstr = message.id.encode(catalog.charset)
183-
else:
184-
msgstr = message.string.encode(catalog.charset)
181+
msgstr = message.string.encode(catalog.charset)
185182
if message.context:
186183
msgid = b'\x04'.join([message.context.encode(catalog.charset),
187184
msgid])

tests/messages/test_mofile.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,26 @@ def test_more_plural_forms(self):
7171
catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
7272
buf = BytesIO()
7373
mofile.write_mo(buf, catalog2)
74+
75+
def test_empty_translation_with_fallback(self):
76+
catalog1 = Catalog(locale='fr_FR')
77+
catalog1.add(u'', '''\
78+
"Content-Type: text/plain; charset=utf-8\n"
79+
"Content-Transfer-Encoding: 8bit\n''')
80+
catalog1.add(u'Fuzz', '')
81+
buf1 = BytesIO()
82+
mofile.write_mo(buf1, catalog1)
83+
buf1.seek(0)
84+
catalog2 = Catalog(locale='fr')
85+
catalog2.add(u'', '''\
86+
"Content-Type: text/plain; charset=utf-8\n"
87+
"Content-Transfer-Encoding: 8bit\n''')
88+
catalog2.add(u'Fuzz', 'Flou')
89+
buf2 = BytesIO()
90+
mofile.write_mo(buf2, catalog2)
91+
buf2.seek(0)
92+
93+
translations = Translations(fp=buf1)
94+
translations.add_fallback(Translations(fp=buf2))
95+
96+
self.assertEqual(u'Flou', translations.ugettext('Fuzz'))

0 commit comments

Comments
 (0)