Author: jezdez
Date: 2011-09-08 06:25:41 -0700 (Thu, 08 Sep 2011)
New Revision: 16730

Modified:
   django/trunk/django/templatetags/i18n.py
   django/trunk/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo
   django/trunk/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po
   django/trunk/tests/regressiontests/i18n/tests.py
Log:
Fixed #16721 -- Made sure that blocktrans correctly handles percents (%), even 
in the plural block. Thanks for the initial patch, Claude Paroz.

Modified: django/trunk/django/templatetags/i18n.py
===================================================================
--- django/trunk/django/templatetags/i18n.py    2011-09-08 13:25:31 UTC (rev 
16729)
+++ django/trunk/django/templatetags/i18n.py    2011-09-08 13:25:41 UTC (rev 
16730)
@@ -116,16 +116,17 @@
         # the end of function
         context.update(tmp_context)
         singular, vars = self.render_token_list(self.singular)
+        # Escape all isolated '%'
+        singular = re.sub(u'%(?!\()', u'%%', singular)
         if self.plural and self.countervar and self.counter:
             count = self.counter.resolve(context)
             context[self.countervar] = count
             plural, plural_vars = self.render_token_list(self.plural)
+            plural = re.sub(u'%(?!\()', u'%%', plural)
             result = translation.ungettext(singular, plural, count)
             vars.extend(plural_vars)
         else:
             result = translation.ugettext(singular)
-        # Escape all isolated '%' before substituting in the context.
-        result = re.sub(u'%(?!\()', u'%%', result)
         data = dict([(v, _render_value_in_context(context.get(v, ''), 
context)) for v in vars])
         context.pop()
         try:

Modified: 
django/trunk/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo
===================================================================
(Binary files differ)

Modified: 
django/trunk/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po
===================================================================
--- 
django/trunk/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po   
    2011-09-08 13:25:31 UTC (rev 16729)
+++ 
django/trunk/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po   
    2011-09-08 13:25:41 UTC (rev 16730)
@@ -40,3 +40,15 @@
 msgid_plural "%d results"
 msgstr[0] "%d Resultat"
 msgstr[1] "%d Resultate"
+
+#: models.py:13
+#, python-format
+msgid "The result was %(percent)s%%"
+msgstr "Das Ergebnis war %(percent)s%%"
+
+#: models.py:13
+#, python-format
+msgid "%(percent)s%% represents %(num)s object"
+msgid_plural "%(percent)s%% represents %(num)s objects"
+msgstr[0] "%(percent)s%% stellt %(num)s Objekt dar"
+msgstr[1] "%(percent)s%% stellt %(num)s Objekte dar"

Modified: django/trunk/tests/regressiontests/i18n/tests.py
===================================================================
--- django/trunk/tests/regressiontests/i18n/tests.py    2011-09-08 13:25:31 UTC 
(rev 16729)
+++ django/trunk/tests/regressiontests/i18n/tests.py    2011-09-08 13:25:41 UTC 
(rev 16730)
@@ -618,6 +618,19 @@
         r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
         self.assertEqual(g(r), 'zh-cn')
 
+    def test_percent_in_translatable_block(self):
+        extended_locale_paths = settings.LOCALE_PATHS + (
+            os.path.join(here, 'other', 'locale'),
+        )
+        with self.settings(LOCALE_PATHS=extended_locale_paths):
+            t_sing = Template("{% load i18n %}{% blocktrans %}The result was 
{{ percent }}%{% endblocktrans %}")
+            t_plur = Template("{% load i18n %}{% blocktrans count num as 
number %}{{ percent }}% represents {{ num }} object{% plural %}{{ percent }}% 
represents {{ num }} objects{% endblocktrans %}")
+            with translation.override('de'):
+                self.assertEqual(t_sing.render(Context({'percent': 42})), 
u'Das Ergebnis war 42%')
+                self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 
1})), u'42% stellt 1 Objekt dar')
+                self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 
4})), u'42% stellt 4 Objekte dar')
+
+
 class ResolutionOrderI18NTests(TestCase):
 
     def setUp(self):

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to