Ticket #16721: 16721.diff

File 16721.diff, 3.6 KB (added by claudep, 4 years ago)

Fix percent escaping in blocktrans rendering

  • django/templatetags/i18n.py

    diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
    index c7202e2..866db44 100644
    a b class BlockTranslateNode(Node): 
    110110        # the end of function
    111111        context.update(tmp_context)
    112112        singular, vars = self.render_token_list(self.singular)
     113        # Escape all isolated '%'
     114        singular = re.sub(u'%(?!\()', u'%%', singular)
    113115        if self.plural and self.countervar and self.counter:
    114116            count = self.counter.resolve(context)
    115117            context[self.countervar] = count
    116118            plural, plural_vars = self.render_token_list(self.plural)
     119            plural = re.sub(u'%(?!\()', u'%%', plural)
    117120            result = translation.ungettext(singular, plural, count)
    118121            vars.extend(plural_vars)
    119122        else:
    120123            result = translation.ugettext(singular)
    121         # Escape all isolated '%' before substituting in the context.
    122         result = re.sub(u'%(?!\()', u'%%', result)
    123124        data = dict([(v, _render_value_in_context(context.get(v, ''), context)) for v in vars])
    124125        context.pop()
    125126        return result % data
  • 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 241de05..72af5d9 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 2d9b89e..1204d80 100644
    a b msgid "%d result" 
    4040msgid_plural "%d results"
    4141msgstr[0] "%d Resultat"
    4242msgstr[1] "%d Resultate"
     43
     44#: models.py:13
     45#, python-format
     46msgid "The result was %(percent)s%%"
     47msgstr "Das Ergebnis war %(percent)s%%"
     48
     49#: models.py:13
     50#, python-format
     51msgid "%(percent)s%% represents %(num)s object"
     52msgid_plural "%(percent)s%% represents %(num)s objects"
     53msgstr[0] "%(percent)s%% darstellt %(num)s Objekt"
     54msgstr[1] "%(percent)s%% darstellt %(num)s Objekte"
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 0b955ad..20302d1 100644
    a b class MiscTests(TestCase): 
    600600        r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
    601601        self.assertEqual(g(r), 'zh-cn')
    602602
     603    def test_percent_in_translatable_block(self):
     604        extended_locale_paths = settings.LOCALE_PATHS + (
     605            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),
     606        )
     607        with self.settings(LOCALE_PATHS=extended_locale_paths):
     608            t_sing = Template("{% load i18n %}{% blocktrans %}The result was {{ percent }}%{% endblocktrans %}")
     609            t_plur = Template("{% load i18n %}{% blocktrans count num as number %}{{ percent }}% represents {{ num }} object{% plural %}{{ percent }}% represents {{ num }} objects{% endblocktrans %}")
     610            with translation.override('de'):
     611                self.assertEqual(t_sing.render(Context({'percent': 42})), u'Das Ergebnis war 42%')
     612                self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 1})), u'42% darstellt 1 Objekt')
     613                self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 4})), u'42% darstellt 4 Objekte')
     614
     615
    603616class ResolutionOrderI18NTests(TestCase):
    604617
    605618    def setUp(self):
Back to Top