Code

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

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