babel
babel copied to clipboard
Compiled .mo files are using msgids for empty translations of ngettext entries, instead of allowing fallback
Overview Description
This commit fixed the issue of replacing empty translations in .mo files with msgid to instead leave empty, to allow fallback. However, I don't see the same behavior for plural entries, and thus ngettext calls are returning the msgids
Steps to Reproduce
- I discovered this when creating test translation classes to detect missing translations like so:
class MissingTranslationsException(Exception):
pass
class TestFallback(gettext.NullTranslations):
"""
This fallback object gets used when a gettext instance can't find translations
for a given message (string).
"""
def gettext(self, msg):
raise MissingTranslationsException
def ngettext(self, singular, plural, n):
raise MissingTranslationsException
class TestTranslations(gettext.GNUTranslations, object):
"""
A special translations class with the fallback that returns Exceptions if translations
can't be found. Fallback must be set to bypass the default behavior of simply
returning the original string as the translation.
"""
def __init__(self, *args, **kwargs):
super(TestTranslations, self).__init__(*args, **kwargs)
self.add_fallback(TestFallback())
translations_module = gettext.translation(
'messages',
localedir=path/to/my/dir,
languages=['de'],
class_=TestTranslations,
)
# Testing the module and fallback
translations_module.ngettext('You have one item', 'You have many items', 1)
translations_module.ngettext('You have one item', 'You have many items', 2)
In the de.po file, the msgstrs for 'You have one item'/'You have many items' are both empty strings
Actual Results
translations_module.ngettext('You have one item', 'You have many items', 1)
> 'You have one item'
translations_module.ngettext('You have one item', 'You have many items', 2)
> 'You have many items'
Expected Results
Both ngettext calls should be hitting the fallback.
Reproducibility
Always
Additional info:
Babel 2.7.0