Opened 8 years ago

Closed 8 years ago

#5885 closed (duplicate)

simple_tag is not compatible with _() syntax in templates

Reported by: Dmitri Fedortchenko <zeraien@…> Owned by: nobody
Component: Template system Version: master
Severity: Keywords: i18n simple_tag translation internationalization templates documentation
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

Creating a simple_tag decorated tag and then passing it a _('text') does not work.

The error produced is:

Failed lookup for key [_('links')]

Code that produces this error:

@register.simple_tag
def title(text):
    return text.title()
{% title _('links') %}

Trace:

Traceback (most recent call last):
File "/sw/lib/python2.5/site-packages/django/template/__init__.py" in render_node
  813. result = node.render(context)
File "/sw/lib/python2.5/site-packages/django/template/__init__.py" in render
  930. resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
File "/sw/lib/python2.5/site-packages/django/template/__init__.py" in resolve
  707. return self._resolve_lookup(context)
File "/sw/lib/python2.5/site-packages/django/template/__init__.py" in _resolve_lookup
  757. raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bit, current)) # missing attribute

I noticed that the following works:

{{ _('Translate Me')|title }}

However the documentation only mentions that the following syntax is possible:

{% some_special_tag _("Page not found") value|yesno:_("yes,no") %}

In any case, I believe that to make the behaviour of the _() syntax more consistent, it should work even with simple_tag.
I realise that my patch may duplicate some code, but it's the best I could come up with at the moment.

Attachments (2)

template_simple_tag_i18n_fix.diff (968 bytes) - added by Dmitri Fedortchenko <zeraien@…> 8 years ago.
The patch to fix simple_tag and i18n _() syntax. It also includes a workaround for UnicodeDecodeErrors that occur sometimes in VariableNotFound tracebacks (as a bonus)
template_simple_tag_i18n_fix.2.diff (1.1 KB) - added by Dmitri Fedortchenko <zeraien@…> 8 years ago.
Previous patch was dirty. This one is a little better. It allows single or double quotes, and throws AttributeError if there are non-matching quotes…

Download all attachments as: .zip

Change History (4)

Changed 8 years ago by Dmitri Fedortchenko <zeraien@…>

The patch to fix simple_tag and i18n _() syntax. It also includes a workaround for UnicodeDecodeErrors that occur sometimes in VariableNotFound tracebacks (as a bonus)

Changed 8 years ago by Dmitri Fedortchenko <zeraien@…>

Previous patch was dirty. This one is a little better. It allows single or double quotes, and throws AttributeError if there are non-matching quotes...

comment:1 Changed 8 years ago by Dmitri Fedortchenko <zeraien@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set

comment:2 Changed 8 years ago by mtredinnick

  • Resolution set to duplicate
  • Status changed from new to closed

Dupe of #4713

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