Opened 8 years ago

Closed 7 years ago

Last modified 4 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 idangazit 7 years ago.
Documents the operation of trans/blocktrans tags
i18n_r8520_after_docsrefactor.patch (1.7 KB) - added by idangazit 7 years ago.
Same patch, applied to trunk post-docs-refactor

Download all attachments as: .zip

Change History (11)

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 7 years ago by nickefford

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 7 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 7 years ago by idangazit

Documents the operation of trans/blocktrans tags

comment:4 Changed 7 years ago by idangazit

  • 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 7 years ago by mtredinnick

  • milestone set to 1.0
  • Triage Stage changed from Accepted to Ready for checkin

comment:6 Changed 7 years ago by mtredinnick

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

Changed 7 years ago by idangazit

Same patch, applied to trunk post-docs-refactor

comment:7 Changed 7 years ago by idangazit

Updated documentation patch to work following docs-refactor in r8506

comment:8 Changed 7 years ago by jacob

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

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

comment:9 Changed 4 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

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