Opened 17 years ago

Closed 16 years ago

Last modified 13 years ago

#5591 closed (fixed)

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

Reported by: elsdoerfer@… 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)

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

Download all attachments as: .zip

Change History (11)

comment:1 by Simon G <dev@…>, 17 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Nick Efford, 16 years ago

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 by miracle2k, 16 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 Idan Gazit, 16 years ago

Attachment: blocktrans_i18n_docs.diff added

Documents the operation of trans/blocktrans tags

comment:4 by Idan Gazit, 16 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 Malcolm Tredinnick, 16 years ago

milestone: 1.0
Triage Stage: AcceptedReady for checkin

comment:6 by Malcolm Tredinnick, 16 years ago

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

by Idan Gazit, 16 years ago

Same patch, applied to trunk post-docs-refactor

comment:7 by Idan Gazit, 16 years ago

Updated documentation patch to work following docs-refactor in r8506

comment:8 by Jacob, 16 years ago

Resolution: fixed
Status: newclosed

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

comment:9 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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