Ticket #19160: 19160.smart-ngettext-lazy.diff
File 19160.smart-ngettext-lazy.diff, 5.0 KB (added by , 12 years ago) |
---|
-
django/utils/translation/__init__.py
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index f3cc634..712a283 100644
a b def npgettext(context, singular, plural, number): 80 80 return _trans.npgettext(context, singular, plural, number) 81 81 82 82 gettext_lazy = lazy(gettext, str) 83 ngettext_lazy = lazy(ngettext, str)84 83 ugettext_lazy = lazy(ugettext, six.text_type) 85 ungettext_lazy = lazy(ungettext, six.text_type)86 84 pgettext_lazy = lazy(pgettext, six.text_type) 87 npgettext_lazy = lazy(npgettext, six.text_type) 85 86 87 def lazy_number(func, resultclasses, number=None, **kwargs): 88 if number is not None: 89 kwargs['number'] = number 90 proxy = lazy(func, *resultclasses)(**kwargs) 91 else: 92 proxy = lazy(func, *resultclasses)(**kwargs) 93 def mod(self, rhs): 94 if isinstance(rhs, dict): 95 number = rhs.values()[0] 96 else: 97 number = rhs 98 self._proxy____kw['number'] = number 99 result = original_mod(self, rhs) 100 del self._proxy____kw['number'] 101 return result 102 original_mod = getattr(proxy.__class__, '__mod__') 103 setattr(proxy.__class__, '__mod__', mod) 104 return proxy 105 106 def ngettext_lazy(singular, plural, number=None): 107 return lazy_number( 108 ngettext, [str], singular=singular, plural=plural, number=number) 109 110 def ungettext_lazy(singular, plural, number=None): 111 return lazy_number( 112 ungettext, [six.text_type], singular=singular, plural=plural, number=number) 113 114 def npgettext_lazy(context, singular, plural, number=None): 115 return lazy_number( 116 npgettext, [six.text_type], context=context, singular=singular, plural=plural, number=number) 117 118 88 119 89 120 def activate(language): 90 121 return _trans.activate(language) -
tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po
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 f825e39..973f0d7 100644 Binary files a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo and b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo differ 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 a471d38..63b5339 100644
a b msgid "May" 35 35 msgstr "Kann" 36 36 37 37 #: models.py:11 38 msgid "%d good result" 39 msgid_plural "%d good results" 40 msgstr[0] "%d gutes Resultat" 41 msgstr[1] "%d guten Resultate" 42 43 #: models.py:11 44 msgid "%(num)d good result" 45 msgid_plural "%(num)d good results" 46 msgstr[0] "%(num)d gutes Resultat" 47 msgstr[1] "%(num)d guten Resultate" 48 49 #: models.py:11 38 50 msgctxt "search" 39 51 msgid "%d result" 40 52 msgid_plural "%d results" … … msgstr "Es gibt %(num_comments)s Kommentare" 75 87 #: models.py:23 76 88 msgctxt "other comment count" 77 89 msgid "There are %(num_comments)s comments" 78 msgstr "Andere: Es gibt %(num_comments)s Kommentare" 79 No newline at end of file 90 msgstr "Andere: Es gibt %(num_comments)s Kommentare" -
tests/regressiontests/i18n/tests.py
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 1346383..96a18fb 100644
a b from django.utils.safestring import mark_safe, SafeBytes, SafeString, SafeText 22 22 from django.utils import six 23 23 from django.utils.six import PY3 24 24 from django.utils.translation import (ugettext, ugettext_lazy, activate, 25 deactivate, gettext_lazy, pgettext, npgettext, to_locale,25 deactivate, gettext_lazy, ungettext_lazy, pgettext, npgettext, to_locale, 26 26 get_language_info, get_language, get_language_from_request, trans_real) 27 27 28 28 … … extended_locale_paths = settings.LOCALE_PATHS + ( 50 50 ) 51 51 52 52 class TranslationTests(TestCase): 53 54 53 def test_override(self): 55 54 activate('de') 56 55 with translation.override('pl'): … … class TranslationTests(TestCase): 90 89 self.assertEqual(six.text_type(s2), "test") 91 90 92 91 @override_settings(LOCALE_PATHS=extended_locale_paths) 92 def test_ungettext_lazy(self): 93 s = ungettext_lazy("%d good result", "%d good result") 94 with translation.override('de'): 95 self.assertEqual(s % 1, "1 gutes Resultat") 96 self.assertEqual(s % 4, "4 guten Resultate") 97 98 s1 = ungettext_lazy("%(num)d good result", "%(num)d good results", 4) 99 s2 = ungettext_lazy("%(num)d good result", "%(num)d good results") 100 with translation.override('de'): 101 self.assertEqual(s1 % {'num': 4}, "4 guten Resultate") 102 self.assertEqual(s2 % {'num': 1}, "1 gutes Resultat") 103 self.assertEqual(s2 % {'num': 4}, "4 guten Resultate") 104 105 @override_settings(LOCALE_PATHS=extended_locale_paths) 93 106 def test_pgettext(self): 94 107 trans_real._active = local() 95 108 trans_real._translations = {}