Ticket #19160: 19160.smart-ngettext-lazy.diff

File 19160.smart-ngettext-lazy.diff, 5.0 KB (added by julien, 2 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): 
    8080    return _trans.npgettext(context, singular, plural, number)
    8181
    8282gettext_lazy = lazy(gettext, str)
    83 ngettext_lazy = lazy(ngettext, str)
    8483ugettext_lazy = lazy(ugettext, six.text_type)
    85 ungettext_lazy = lazy(ungettext, six.text_type)
    8684pgettext_lazy = lazy(pgettext, six.text_type)
    87 npgettext_lazy = lazy(npgettext, six.text_type)
     85
     86
     87def 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
     106def ngettext_lazy(singular, plural, number=None):
     107    return lazy_number(
     108        ngettext, [str], singular=singular, plural=plural, number=number)
     109
     110def ungettext_lazy(singular, plural, number=None):
     111    return lazy_number(
     112        ungettext, [six.text_type], singular=singular, plural=plural, number=number)
     113
     114def 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
    88119
    89120def activate(language):
    90121    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" 
    3535msgstr "Kann"
    3636
    3737#: models.py:11
     38msgid "%d good result"
     39msgid_plural "%d good results"
     40msgstr[0] "%d gutes Resultat"
     41msgstr[1] "%d guten Resultate"
     42
     43#: models.py:11
     44msgid "%(num)d good result"
     45msgid_plural "%(num)d good results"
     46msgstr[0] "%(num)d gutes Resultat"
     47msgstr[1] "%(num)d guten Resultate"
     48
     49#: models.py:11
    3850msgctxt "search"
    3951msgid "%d result"
    4052msgid_plural "%d results"
    msgstr "Es gibt %(num_comments)s Kommentare" 
    7587#: models.py:23
    7688msgctxt "other comment count"
    7789msgid "There are %(num_comments)s comments"
    78 msgstr "Andere: Es gibt %(num_comments)s Kommentare"
    79  No newline at end of file
     90msgstr "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 
    2222from django.utils import six
    2323from django.utils.six import PY3
    2424from 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,
    2626    get_language_info, get_language, get_language_from_request, trans_real)
    2727
    2828
    extended_locale_paths = settings.LOCALE_PATHS + ( 
    5050)
    5151
    5252class TranslationTests(TestCase):
    53 
    5453    def test_override(self):
    5554        activate('de')
    5655        with translation.override('pl'):
    class TranslationTests(TestCase): 
    9089        self.assertEqual(six.text_type(s2), "test")
    9190
    9291    @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)
    93106    def test_pgettext(self):
    94107        trans_real._active = local()
    95108        trans_real._translations = {}
Back to Top