Opened 8 years ago

Closed 4 years ago

Last modified 18 months ago

#4982 closed Uncategorized (fixed)

{% blocktrans %} does not escape % in text

Reported by: nostgard <nostgard@…> Owned by: permon
Component: Internationalization Version: master
Severity: Normal Keywords: sprintsept14
Cc: permonik@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

When you try to use a blocktrans with a percent sign in it. When the percent is escaped (%%) it renders fine. It does not occur when using a trans tag.

>>> from django.template import Context, Template
>>> t1 = Template('{% load i18n %}{% blocktrans %}We lost 5% today.{% endblocktrans %}')
>>> t2 = Template('{% load i18n %}{% blocktrans %}We lost 5%% today.{% endblocktrans %}')
>>> t3 = Template('{% load i18n %}{% trans "We lost 5% today." %}')
>>> c = Context()
>>> t1.render(c)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\Python\lib\site-packages\django\template\__init__.py", line 181, in render
    return self.nodelist.render(context)
  File "D:\Python\lib\site-packages\django\template\__init__.py", line 736, in render
    bits.append(self.render_node(node, context))
  File "D:\Python\lib\site-packages\django\template\__init__.py", line 764, in render_node
    raise wrapped
TemplateSyntaxError: Caught an exception while rendering: unsupported format character 't' (0x74) at index 11

Original Traceback (most recent call last):
  File "D:\Python\lib\site-packages\django\template\__init__.py", line 754, in render_node
    result = node.render(context)
  File "D:\Python\lib\site-packages\django\templatetags\i18n.py", line 73, in render
    result = translation.ugettext(singular) % context
ValueError: unsupported format character 't' (0x74) at index 11

>>> t2.render(c)
u'We lost 5% today.'
>>> t3.render(c)
u'We lost 5% today.'

Attachments (1)

patch.diff (1016 bytes) - added by permonik@… 7 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 8 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 8 years ago by John Shaffer <jshaffer2112@…>

  • Component changed from Translations to Internationalization

Changed 7 years ago by permonik@…

comment:3 Changed 7 years ago by permonik@…

  • Has patch set

Blocktrans creates template string with %()s, but doesn't correctly escape % signs in original text. Patch attached, make-messages.py works as intended - so no other harm in i18n machinery.

comment:4 Changed 7 years ago by anonymous

  • Cc permonik@… added

comment:5 Changed 7 years ago by permon

  • Owner changed from nobody to permon
  • Status changed from new to assigned

comment:6 Changed 7 years ago by permon

  • Keywords sprintsept14 added

comment:7 Changed 7 years ago by permon

  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [6565]) Fixed #4982 -- Fixed handling of '%' symbols in 'blocktrans' blocks. Thanks,
permonik@….

comment:9 follow-up: Changed 5 years ago by nasmon

  • Patch needs improvement set
  • Resolution fixed deleted
  • Status changed from closed to reopened

On Django 1.2.1:
When using single % inside blocktrans, "django-admin.py makemessages" won't work and as %% inside the template, the output still is %%, due to the patch above. When removing the patch line, everything works ok.

comment:10 in reply to: ↑ 9 Changed 5 years ago by permon

  • Cc permonik@… added; permonik@… removed

Replying to nasmon:

On Django 1.2.1:
When using single % inside blocktrans, "django-admin.py makemessages" won't work and as %% inside the template, the output still is %%, due to the patch above. When removing the patch line, everything works ok.

Can you attach some working example? For me is everything working ok with 1.2.1 tree.

comment:11 Changed 4 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from reopened to closed
  • Triage Stage changed from Ready for checkin to Accepted

I can't repeat this problem, either. Closing for now. Please open a new ticket if there's a specific failure case, because we're not going to revert this change -- at the time it was applied, it fixed a concrete problem -- so it's a matter of tweaking the current code to fix things if there's a concrete failure case.

Reclosing as "fixed" to restore previous state.

comment:12 Changed 4 years ago by claudep

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

FYI, I've reopened the issue with patch/test case in #16721.

comment:13 Changed 18 months ago by Shailen Sobhee

The bug is still present in Django 1.5.2

Note: See TracTickets for help on using tickets.
Back to Top