Ticket #19160: 19160-4.diff

File 19160-4.diff, 6.8 KB (added by Claude Paroz, 11 years ago)

Aggregated patch

  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    index f3cc634..05440e5 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 isinstance(number, int):
     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) and key:
     95                number = rhs[key]
     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        key = number
     103        original_mod = getattr(proxy.__class__, '__mod__')
     104        setattr(proxy.__class__, '__mod__', mod)
     105    return proxy
     106
     107def ngettext_lazy(singular, plural, number=None):
     108    return lazy_number(
     109        ngettext, [str], singular=singular, plural=plural, number=number)
     110
     111def ungettext_lazy(singular, plural, number=None):
     112    return lazy_number(
     113        ungettext, [six.text_type], singular=singular, plural=plural, number=number)
     114
     115def npgettext_lazy(context, singular, plural, number=None):
     116    return lazy_number(
     117        npgettext, [six.text_type], context=context, singular=singular, plural=plural, number=number)
     118
     119
    88120
    89121def activate(language):
    90122    return _trans.activate(language)
  • docs/topics/i18n/translation.txt

    diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt
    index 65c6fe2..ad7e9d4 100644
    a b convert them to strings, because they should be converted as late as possible  
    408408(so that the correct locale is in effect). This necessitates the use of the
    409409helper function described next.
    410410
     411Lazy translations and plural
     412----------------------------
     413
     414.. versionadded:: 1.5
     415
     416When using lazy translation for a plural string (``[u]n[p]gettext_lazy``), you
     417generally don't know the ``number`` argument at the time of the string
     418definition. Therefore, you are authorized to pass a dictionary key name in place
     419of an integer as the ``number`` argument. Then, when the string is effectively
     420translated with a placeholders dictionary, the ``number`` argument will get
     421substituted by the value of the key in the dictionary. Alternatively, if the
     422string contains only one unnamed placeholder, you can also omit passing the
     423``number`` argument at all. For example::
     424
     425    class MyForm(forms.Form):
     426        error1_message = ungettext_lazy("You only provided %(num)d argument",
     427            "You only provided %(num)d arguments", 'num')
     428        error2_message = ungettext_lazy("You provided %d argument",
     429            "You provided %d arguments")
     430
     431        def clean(self):
     432            if err_1:
     433                raise forms.ValidationError(self.error1_message % {'num': number})
     434            if err_2:
     435                raise forms.ValidationError(self.error2_message % number)
     436
     437
    411438Joining strings: string_concat()
    412439~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    413440
  • 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..b43f282 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..e138bb0 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 "Hi %(name)s, %(num)d good result"
     45msgid_plural "Hi %(name)s, %(num)d good results"
     46msgstr[0] "Hallo %(name)s, %(num)d gutes Resultat"
     47msgstr[1] "Hallo %(name)s, %(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 dcc288e..50654df 100644
    a b from django.utils.safestring import mark_safe, SafeBytes, SafeString, SafeText  
    2323from django.utils import six
    2424from django.utils.six import PY3
    2525from django.utils.translation import (ugettext, ugettext_lazy, activate,
    26     deactivate, gettext_lazy, pgettext, npgettext, to_locale,
     26    deactivate, gettext_lazy, ungettext_lazy, pgettext, npgettext, to_locale,
    2727    get_language_info, get_language, get_language_from_request, trans_real)
    2828
    2929
    extended_locale_paths = settings.LOCALE_PATHS + (  
    5151)
    5252
    5353class TranslationTests(TestCase):
    54 
    5554    def test_override(self):
    5655        activate('de')
    5756        with translation.override('pl'):
    class TranslationTests(TestCase):  
    9594        self.assertEqual(six.text_type(s2), "test")
    9695
    9796    @override_settings(LOCALE_PATHS=extended_locale_paths)
     97    def test_ungettext_lazy(self):
     98        s = ungettext_lazy("%d good result", "%d good result")
     99        with translation.override('de'):
     100            self.assertEqual(s % 1, "1 gutes Resultat")
     101            self.assertEqual(s % 4, "4 guten Resultate")
     102
     103        s1 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 4)
     104        s2 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 'num')
     105        with translation.override('de'):
     106            self.assertEqual(s1 % {'num': 4, 'name': 'Jim'}, "Hallo Jim, 4 guten Resultate")
     107            self.assertEqual(s2 % {'name': 'Jim', 'num': 1}, "Hallo Jim, 1 gutes Resultat")
     108            self.assertEqual(s2 % {'name': 'Jim', 'num': 5}, "Hallo Jim, 5 guten Resultate")
     109
     110    @override_settings(LOCALE_PATHS=extended_locale_paths)
    98111    def test_pgettext(self):
    99112        trans_real._active = local()
    100113        trans_real._translations = {}
Back to Top