Opened 5 years ago

Last modified 5 years ago

#30718 closed Bug

blocktrans is crashing when using percent signs when no variables were supplied — at Initial Version

Reported by: Mikail Owned by: nobody
Component: Internationalization Version: dev
Severity: Normal Keywords: i18n blocktrans
Cc: Claude Paroz Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using blocktrans without supplying any variables and a translator puts a percent sign as a translation, a crash occurs.

For example, using this template snippet:

{% blocktrans context "Taxes" %}
  tax_rate * price
{% endblocktrans %}

If one translates it to:

  % imposto * preço

It will raise:

TypeError: %i format: a number is required, not dict

Where stack is:

  File "django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "django/template/base.py", line 171, in render
    return self._render(context)
  File "django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "django/templatetags/i18n.py", line 161, in render
    result = result % data

The crashing line being this one: 1e429df7488/django/templatetags/i18n.py#L161

The PO compiler doesn't check the validity of the provided translation as the flag python-format flag is not generated in the PO file is not set.

We should whether not try to format the string if there is no variable, or force the python-format flag to be set to prevent translators from breaking the templates.

Change History (0)

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