From ace9d4efc3e6e0b07fd94b039d62e0d32c81ed3f Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 2 Feb 2013 10:56:41 +0100 Subject: [PATCH] Made ungettext_lazy usable for messages that do not contain the count. Fixed #19160 (again). Thanks Alexey Boriskin. --- django/utils/translation/__init__.py | 8 ++- .../other/locale/de/LC_MESSAGES/django.mo | Bin 1998 -> 2070 bytes .../other/locale/de/LC_MESSAGES/django.po | 6 ++ tests/regressiontests/i18n/tests.py | 57 +++++++++--------- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 7a48376a52..6fd52b6023 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -106,7 +106,13 @@ def lazy_number(func, resultclass, number=None, **kwargs): else: number_value = rhs kwargs['number'] = number_value - return func(**kwargs) % rhs + translated = func(**kwargs) + try: + translated = translated % rhs + except TypeError: + # String doesn't contain a placeholder for the number + pass + return translated proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs) return proxy diff --git a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo index e208c8249f6b334e54e8a1f8061f9f60383d8874..ef64b650ec8b1f316a2d1162f32d66eecb857338 100644 GIT binary patch delta 432 zcmYMwy-EW?6o%n%va1_^H>;o#e+Vcdi6mHvSlFmd5Jjy73kev~h(VKp2$D9b7p+rx z2O*_RIuQgbA%fn4Wxz&!CxQ-q`<$79nX~ik-^Hr$WA3dO0huB9WRff!K8zafV;$$P zg9G@5NpvF84yJJvuhE|KfG&1$1wSy3fg_D#64#`v)+x4FxWH*_;Re276n#!P;j%c| zMy~p#1$@LQ{KQ3!_De^Y!9A>@ZTJ<(v5WIK=>4vz(b2KAOOayZ0Jrf5ZKrKq#W&<` z1$09nz1sc(_iC8f*PvGo5j%WhUvnhPnv@%(3L9pr*)TaXQ79Hq6PNi?<*cm#y7?XP Y+}T2O(RRq&}7Y=weHrR?*XYcBZ>e3 diff --git a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po index 3676893ca3..822fa3796b 100644 --- a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po +++ b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po @@ -47,6 +47,12 @@ msgid_plural "%d good results" msgstr[0] "%d gutes Resultat" msgstr[1] "%d guten Resultate" +#: models.py:11 +msgid "good result" +msgid_plural "good results" +msgstr[0] "gutes Resultat" +msgstr[1] "guten Resultate" + #: models.py:11 msgctxt "Exclamation" msgid "%d good result" diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index fc1a832b89..7b4fd0eff8 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -102,39 +102,42 @@ class TranslationTests(TestCase): @override_settings(LOCALE_PATHS=extended_locale_paths) def test_ungettext_lazy(self): - s0 = ungettext_lazy("%d good result", "%d good results") - s1 = ngettext_lazy(str("%d good result"), str("%d good results")) - s2 = npgettext_lazy('Exclamation', '%d good result', '%d good results') + simple_with_format = ungettext_lazy('%d good result', '%d good results') + simple_str_with_format = ngettext_lazy(str('%d good result'), str('%d good results')) + simple_context_with_format = npgettext_lazy('Exclamation', '%d good result', '%d good results') + simple_without_format = ungettext_lazy('good result', 'good results') with translation.override('de'): - self.assertEqual(s0 % 1, "1 gutes Resultat") - self.assertEqual(s0 % 4, "4 guten Resultate") - self.assertEqual(s1 % 1, str("1 gutes Resultat")) - self.assertEqual(s1 % 4, str("4 guten Resultate")) - self.assertEqual(s2 % 1, "1 gutes Resultat!") - self.assertEqual(s2 % 4, "4 guten Resultate!") + self.assertEqual(simple_with_format % 1, '1 gutes Resultat') + self.assertEqual(simple_with_format % 4, '4 guten Resultate') + self.assertEqual(simple_str_with_format % 1, str('1 gutes Resultat')) + self.assertEqual(simple_str_with_format % 4, str('4 guten Resultate')) + self.assertEqual(simple_context_with_format % 1, '1 gutes Resultat!') + self.assertEqual(simple_context_with_format % 4, '4 guten Resultate!') + self.assertEqual(simple_without_format % 1, 'gutes Resultat') + self.assertEqual(simple_without_format % 4, 'guten Resultate') - s3 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 4) - s4 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 'num') - s5 = ngettext_lazy(str("Hi %(name)s, %(num)d good result"), str("Hi %(name)s, %(num)d good results"), 4) - s6 = ngettext_lazy(str("Hi %(name)s, %(num)d good result"), str("Hi %(name)s, %(num)d good results"), 'num') - s7 = npgettext_lazy('Greeting', "Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 4) - s8 = npgettext_lazy('Greeting', "Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 'num') + complex_nonlazy = ungettext_lazy('Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 4) + complex_deferred = ungettext_lazy('Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 'num') + complex_str_nonlazy = ngettext_lazy(str('Hi %(name)s, %(num)d good result'), str('Hi %(name)s, %(num)d good results'), 4) + complex_str_deferred = ngettext_lazy(str('Hi %(name)s, %(num)d good result'), str('Hi %(name)s, %(num)d good results'), 'num') + complex_context_nonlazy = npgettext_lazy('Greeting', 'Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 4) + complex_context_deferred = npgettext_lazy('Greeting', 'Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 'num') with translation.override('de'): - self.assertEqual(s3 % {'num': 4, 'name': 'Jim'}, "Hallo Jim, 4 guten Resultate") - self.assertEqual(s4 % {'name': 'Jim', 'num': 1}, "Hallo Jim, 1 gutes Resultat") - self.assertEqual(s4 % {'name': 'Jim', 'num': 5}, "Hallo Jim, 5 guten Resultate") + self.assertEqual(complex_nonlazy % {'num': 4, 'name': 'Jim'}, 'Hallo Jim, 4 guten Resultate') + self.assertEqual(complex_deferred % {'name': 'Jim', 'num': 1}, 'Hallo Jim, 1 gutes Resultat') + self.assertEqual(complex_deferred % {'name': 'Jim', 'num': 5}, 'Hallo Jim, 5 guten Resultate') with six.assertRaisesRegex(self, KeyError, 'Your dictionary lacks key.*'): - s4 % {'name': 'Jim'} - self.assertEqual(s5 % {'num': 4, 'name': 'Jim'}, str("Hallo Jim, 4 guten Resultate")) - self.assertEqual(s6 % {'name': 'Jim', 'num': 1}, str("Hallo Jim, 1 gutes Resultat")) - self.assertEqual(s6 % {'name': 'Jim', 'num': 5}, str("Hallo Jim, 5 guten Resultate")) + complex_deferred % {'name': 'Jim'} + self.assertEqual(complex_str_nonlazy % {'num': 4, 'name': 'Jim'}, str('Hallo Jim, 4 guten Resultate')) + self.assertEqual(complex_str_deferred % {'name': 'Jim', 'num': 1}, str('Hallo Jim, 1 gutes Resultat')) + self.assertEqual(complex_str_deferred % {'name': 'Jim', 'num': 5}, str('Hallo Jim, 5 guten Resultate')) with six.assertRaisesRegex(self, KeyError, 'Your dictionary lacks key.*'): - s6 % {'name': 'Jim'} - self.assertEqual(s7 % {'num': 4, 'name': 'Jim'}, "Willkommen Jim, 4 guten Resultate") - self.assertEqual(s8 % {'name': 'Jim', 'num': 1}, "Willkommen Jim, 1 gutes Resultat") - self.assertEqual(s8 % {'name': 'Jim', 'num': 5}, "Willkommen Jim, 5 guten Resultate") + complex_str_deferred % {'name': 'Jim'} + self.assertEqual(complex_context_nonlazy % {'num': 4, 'name': 'Jim'}, 'Willkommen Jim, 4 guten Resultate') + self.assertEqual(complex_context_deferred % {'name': 'Jim', 'num': 1}, 'Willkommen Jim, 1 gutes Resultat') + self.assertEqual(complex_context_deferred % {'name': 'Jim', 'num': 5}, 'Willkommen Jim, 5 guten Resultate') with six.assertRaisesRegex(self, KeyError, 'Your dictionary lacks key.*'): - s8 % {'name': 'Jim'} + complex_context_deferred % {'name': 'Jim'} @override_settings(LOCALE_PATHS=extended_locale_paths) def test_pgettext(self):