Opened 9 years ago

Closed 8 years ago

Last modified 5 years ago

#5591 closed (fixed)

i18n documentation: Incorrect statement about template variables in {% trans %}

Reported by: elsdoerfer@… Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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)

blocktrans_i18n_docs.diff (1.7 KB) - added by Idan Gazit 8 years ago.
Documents the operation of trans/blocktrans tags
i18n_r8520_after_docsrefactor.patch (1.7 KB) - added by Idan Gazit 8 years ago.
Same patch, applied to trunk post-docs-refactor

Download all attachments as: .zip

Change History (11)

comment:1 Changed 9 years ago by Simon G <dev@…>

Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by Nick Efford

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 a
TemplateSyntaxError with the message only 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 :)

comment:3 Changed 8 years ago by miracle2k

Using current SVN (but no actual translations loaded):

In [17]: Template('{% load i18n %}{% trans var %}').render({'var': "foobar"})
Out[17]: u'foobar'

Changed 8 years ago by Idan Gazit

Attachment: blocktrans_i18n_docs.diff added

Documents the operation of trans/blocktrans tags

comment:4 Changed 8 years ago by Idan Gazit

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 Changed 8 years ago by Malcolm Tredinnick

milestone: 1.0
Triage Stage: AcceptedReady for checkin

comment:6 Changed 8 years ago by Malcolm Tredinnick

Should wait until after docs-refactor lands to be committed (at which point the patch should be rewritte).

Changed 8 years ago by Idan Gazit

Same patch, applied to trunk post-docs-refactor

comment:7 Changed 8 years ago by Idan Gazit

Updated documentation patch to work following docs-refactor in r8506

comment:8 Changed 8 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [8551]) Fixed #5591: corrected info about variables in trans tag. Thanks, idangazit.

comment:9 Changed 5 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

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