[1.8.x] Fixed #24515 -- Fixed DjangoTranslation plural handling

Backport of 9e83f30cd3 from master.
This commit is contained in:
Claude Paroz 2015-03-20 19:45:53 +01:00
parent 744d9a10ef
commit 8e4b0d6010
2 changed files with 13 additions and 2 deletions

View File

@ -111,7 +111,6 @@ class DjangoTranslation(gettext_module.GNUTranslations):
self.__language = language self.__language = language
self.__to_language = to_language(language) self.__to_language = to_language(language)
self.__locale = to_locale(language) self.__locale = to_locale(language)
self.plural = lambda n: int(n != 1)
self._init_translation_catalog() self._init_translation_catalog()
self._add_installed_apps_translations() self._add_installed_apps_translations()
@ -139,6 +138,7 @@ class DjangoTranslation(gettext_module.GNUTranslations):
# provides merge support for NullTranslations() # provides merge support for NullTranslations()
translation._catalog = {} translation._catalog = {}
translation._info = {} translation._info = {}
translation.plural = lambda n: int(n != 1)
return translation return translation
def _init_translation_catalog(self): def _init_translation_catalog(self):
@ -151,6 +151,7 @@ class DjangoTranslation(gettext_module.GNUTranslations):
# gettext will raise an IOError (refs #18192). # gettext will raise an IOError (refs #18192).
use_null_fallback = False use_null_fallback = False
translation = self._new_gnu_trans(localedir, use_null_fallback) translation = self._new_gnu_trans(localedir, use_null_fallback)
self.plural = translation.plural
self._info = translation._info.copy() self._info = translation._info.copy()
self._catalog = translation._catalog.copy() self._catalog = translation._catalog.copy()

View File

@ -29,7 +29,7 @@ from django.utils.translation import (
get_language, get_language_from_request, get_language_info, gettext, get_language, get_language_from_request, get_language_info, gettext,
gettext_lazy, ngettext_lazy, npgettext, npgettext_lazy, pgettext, gettext_lazy, ngettext_lazy, npgettext, npgettext_lazy, pgettext,
pgettext_lazy, string_concat, to_locale, trans_real, ugettext, pgettext_lazy, string_concat, to_locale, trans_real, ugettext,
ugettext_lazy, ungettext_lazy, ugettext_lazy, ungettext, ungettext_lazy,
) )
from .forms import CompanyForm, I18nForm, SelectDateForm, SelectDateWidget from .forms import CompanyForm, I18nForm, SelectDateForm, SelectDateWidget
@ -56,6 +56,16 @@ def patch_formats(lang, **settings):
class TranslationTests(TestCase): class TranslationTests(TestCase):
@translation.override('fr')
def test_plural(self):
"""
Test plurals with ungettext. French differs from English in that 0 is singular.
"""
self.assertEqual(ungettext("%d year", "%d years", 0) % 0, "0 année")
self.assertEqual(ungettext("%d year", "%d years", 2) % 2, "2 années")
self.assertEqual(ungettext("%(size)d byte", "%(size)d bytes", 0) % {'size': 0}, "0 octet")
self.assertEqual(ungettext("%(size)d byte", "%(size)d bytes", 2) % {'size': 2}, "2 octets")
def test_override(self): def test_override(self):
activate('de') activate('de')
try: try: