Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made ungettext_lazy usable for messages that do not contain the count.

Fixed #19160 (again). Thanks Alexey Boriskin.
  • Loading branch information...
commit ace9d4efc3e6e0b07fd94b039d62e0d32c81ed3f 1 parent db5049c
Aymeric Augustin authored February 02, 2013
8  django/utils/translation/__init__.py
@@ -106,7 +106,13 @@ def __mod__(self, rhs):
106 106
                 else:
107 107
                     number_value = rhs
108 108
                 kwargs['number'] = number_value
109  
-                return func(**kwargs) % rhs
  109
+                translated = func(**kwargs)
  110
+                try:
  111
+                    translated = translated % rhs
  112
+                except TypeError:
  113
+                    # String doesn't contain a placeholder for the number
  114
+                    pass
  115
+                return translated
110 116
 
111 117
         proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
112 118
     return proxy
BIN  tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo
Binary file not shown
6  tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po
@@ -48,6 +48,12 @@ msgstr[0] "%d gutes Resultat"
48 48
 msgstr[1] "%d guten Resultate"
49 49
 
50 50
 #: models.py:11
  51
+msgid "good result"
  52
+msgid_plural "good results"
  53
+msgstr[0] "gutes Resultat"
  54
+msgstr[1] "guten Resultate"
  55
+
  56
+#: models.py:11
51 57
 msgctxt "Exclamation"
52 58
 msgid "%d good result"
53 59
 msgid_plural "%d good results"
59  tests/regressiontests/i18n/tests.py
@@ -102,39 +102,42 @@ def test_lazy_pickle(self):
102 102
 
103 103
     @override_settings(LOCALE_PATHS=extended_locale_paths)
104 104
     def test_ungettext_lazy(self):
105  
-        s0 = ungettext_lazy("%d good result", "%d good results")
106  
-        s1 = ngettext_lazy(str("%d good result"), str("%d good results"))
107  
-        s2 = npgettext_lazy('Exclamation', '%d good result', '%d good results')
  105
+        simple_with_format = ungettext_lazy('%d good result', '%d good results')
  106
+        simple_str_with_format = ngettext_lazy(str('%d good result'), str('%d good results'))
  107
+        simple_context_with_format = npgettext_lazy('Exclamation', '%d good result', '%d good results')
  108
+        simple_without_format = ungettext_lazy('good result', 'good results')
108 109
         with translation.override('de'):
109  
-            self.assertEqual(s0 % 1, "1 gutes Resultat")
110  
-            self.assertEqual(s0 % 4, "4 guten Resultate")
111  
-            self.assertEqual(s1 % 1, str("1 gutes Resultat"))
112  
-            self.assertEqual(s1 % 4, str("4 guten Resultate"))
113  
-            self.assertEqual(s2 % 1, "1 gutes Resultat!")
114  
-            self.assertEqual(s2 % 4, "4 guten Resultate!")
115  
-
116  
-        s3 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 4)
117  
-        s4 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 'num')
118  
-        s5 = ngettext_lazy(str("Hi %(name)s, %(num)d good result"), str("Hi %(name)s, %(num)d good results"), 4)
119  
-        s6 = ngettext_lazy(str("Hi %(name)s, %(num)d good result"), str("Hi %(name)s, %(num)d good results"), 'num')
120  
-        s7 = npgettext_lazy('Greeting', "Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 4)
121  
-        s8 = npgettext_lazy('Greeting', "Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 'num')
  110
+            self.assertEqual(simple_with_format % 1, '1 gutes Resultat')
  111
+            self.assertEqual(simple_with_format % 4, '4 guten Resultate')
  112
+            self.assertEqual(simple_str_with_format % 1, str('1 gutes Resultat'))
  113
+            self.assertEqual(simple_str_with_format % 4, str('4 guten Resultate'))
  114
+            self.assertEqual(simple_context_with_format % 1, '1 gutes Resultat!')
  115
+            self.assertEqual(simple_context_with_format % 4, '4 guten Resultate!')
  116
+            self.assertEqual(simple_without_format % 1, 'gutes Resultat')
  117
+            self.assertEqual(simple_without_format % 4, 'guten Resultate')
  118
+
  119
+        complex_nonlazy = ungettext_lazy('Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 4)
  120
+        complex_deferred = ungettext_lazy('Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 'num')
  121
+        complex_str_nonlazy = ngettext_lazy(str('Hi %(name)s, %(num)d good result'), str('Hi %(name)s, %(num)d good results'), 4)
  122
+        complex_str_deferred = ngettext_lazy(str('Hi %(name)s, %(num)d good result'), str('Hi %(name)s, %(num)d good results'), 'num')
  123
+        complex_context_nonlazy = npgettext_lazy('Greeting', 'Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 4)
  124
+        complex_context_deferred = npgettext_lazy('Greeting', 'Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 'num')
122 125
         with translation.override('de'):
123  
-            self.assertEqual(s3 % {'num': 4, 'name': 'Jim'}, "Hallo Jim, 4 guten Resultate")
124  
-            self.assertEqual(s4 % {'name': 'Jim', 'num': 1}, "Hallo Jim, 1 gutes Resultat")
125  
-            self.assertEqual(s4 % {'name': 'Jim', 'num': 5}, "Hallo Jim, 5 guten Resultate")
  126
+            self.assertEqual(complex_nonlazy % {'num': 4, 'name': 'Jim'}, 'Hallo Jim, 4 guten Resultate')
  127
+            self.assertEqual(complex_deferred % {'name': 'Jim', 'num': 1}, 'Hallo Jim, 1 gutes Resultat')
  128
+            self.assertEqual(complex_deferred % {'name': 'Jim', 'num': 5}, 'Hallo Jim, 5 guten Resultate')
126 129
             with six.assertRaisesRegex(self, KeyError, 'Your dictionary lacks key.*'):
127  
-                s4 % {'name': 'Jim'}
128  
-            self.assertEqual(s5 % {'num': 4, 'name': 'Jim'}, str("Hallo Jim, 4 guten Resultate"))
129  
-            self.assertEqual(s6 % {'name': 'Jim', 'num': 1}, str("Hallo Jim, 1 gutes Resultat"))
130  
-            self.assertEqual(s6 % {'name': 'Jim', 'num': 5}, str("Hallo Jim, 5 guten Resultate"))
  130
+                complex_deferred % {'name': 'Jim'}
  131
+            self.assertEqual(complex_str_nonlazy % {'num': 4, 'name': 'Jim'}, str('Hallo Jim, 4 guten Resultate'))
  132
+            self.assertEqual(complex_str_deferred % {'name': 'Jim', 'num': 1}, str('Hallo Jim, 1 gutes Resultat'))
  133
+            self.assertEqual(complex_str_deferred % {'name': 'Jim', 'num': 5}, str('Hallo Jim, 5 guten Resultate'))
131 134
             with six.assertRaisesRegex(self, KeyError, 'Your dictionary lacks key.*'):
132  
-                s6 % {'name': 'Jim'}
133  
-            self.assertEqual(s7 % {'num': 4, 'name': 'Jim'}, "Willkommen Jim, 4 guten Resultate")
134  
-            self.assertEqual(s8 % {'name': 'Jim', 'num': 1}, "Willkommen Jim, 1 gutes Resultat")
135  
-            self.assertEqual(s8 % {'name': 'Jim', 'num': 5}, "Willkommen Jim, 5 guten Resultate")
  135
+                complex_str_deferred % {'name': 'Jim'}
  136
+            self.assertEqual(complex_context_nonlazy % {'num': 4, 'name': 'Jim'}, 'Willkommen Jim, 4 guten Resultate')
  137
+            self.assertEqual(complex_context_deferred % {'name': 'Jim', 'num': 1}, 'Willkommen Jim, 1 gutes Resultat')
  138
+            self.assertEqual(complex_context_deferred % {'name': 'Jim', 'num': 5}, 'Willkommen Jim, 5 guten Resultate')
136 139
             with six.assertRaisesRegex(self, KeyError, 'Your dictionary lacks key.*'):
137  
-                s8 % {'name': 'Jim'}
  140
+                complex_context_deferred % {'name': 'Jim'}
138 141
 
139 142
     @override_settings(LOCALE_PATHS=extended_locale_paths)
140 143
     def test_pgettext(self):

0 notes on commit ace9d4e

Please sign in to comment.
Something went wrong with that request. Please try again.