#5591 closed (fixed)
i18n documentation: Incorrect statement about template variables in {% trans %}
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Documentation | Version: | dev |
| Severity: | Keywords: | ||
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The documentation at:
http://www.djangoproject.com/documentation/i18n/#in-template-code
states:
It’s not possible to use template variables in {% trans %} — only constant strings, in single or double quotes, are allowed.
However, it works for me (django.templatetags.i18n.TranslateNode.render() includes a call to resolve_variable).
Attachments (2)
Change History (11)
comment:1 by , 18 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 17 years ago
comment:3 by , 17 years ago
Using current SVN (but no actual translations loaded):
In [17]: Template('{% load i18n %}{% trans var %}').render({'var': "foobar"})
Out[17]: u'foobar'
by , 17 years ago
| Attachment: | blocktrans_i18n_docs.diff added |
|---|
Documents the operation of trans/blocktrans tags
comment:4 by , 17 years ago
| Has patch: | set |
|---|
The {% trans %} template tag translates either a constant string (enclosed in single or double quotes) or variable content. It's not possible to mix a template variable inside a string within {% trans %}. If your translations require strings with variables (placeholders),
use {% blocktrans %}.
>>> Template('{% load i18n %}{% trans "this is {{var}} for you" %}').render({'var': "foobar"})
u'this is {{var}} for you'
>>> Template('{% load i18n %}{% blocktrans %}This will have {{ var }} inside.{% endblocktrans %}').render(Context({'var': "foobar"}))
u'This will have foobar inside.'
Patch for clearer documentation attached, including a clearer noop explanation.
comment:5 by , 17 years ago
| milestone: | → 1.0 |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
comment:6 by , 17 years ago
Should wait until after docs-refactor lands to be committed (at which point the patch should be rewritte).
by , 17 years ago
| Attachment: | i18n_r8520_after_docsrefactor.patch added |
|---|
Same patch, applied to trunk post-docs-refactor
comment:8 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Any more info available on this? I am unable to reproduce this.
I tried passing a template variable as the string argument of
{% trans %}, and I also tried embedding a template variable in the string passed to{% trans %}. In the latter case, the variable is simply ignored, and in the former case I see aTemplateSyntaxErrorwith the messageonly option for 'trans' is 'noop'.In other words, the docs seem consistent with what I observe - unless I am being completely dense and not getting the point here (which to be honest occurs frequently :)