Django

Code

Ticket #5591 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: elsdoerfer@gmail.com Assigned to: nobody
Milestone: 1.0 Component: Documentation
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

blocktrans_i18n_docs.diff (1.7 kB) - added by idangazit on 08/17/08 08:48:30.
Documents the operation of trans/blocktrans tags
i18n_r8520_after_docsrefactor.patch (1.7 kB) - added by idangazit on 08/24/08 10:51:40.
Same patch, applied to trunk post-docs-refactor

Change History

12/01/07 20:42:42 changed by Simon G <dev@simon.net.nz>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

07/18/08 13:42:01 changed 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 :)

07/20/08 11:01:04 changed by miracle2k

Using current SVN (but no actual translations loaded):

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

08/17/08 08:48:30 changed by idangazit

  • attachment blocktrans_i18n_docs.diff added.

Documents the operation of trans/blocktrans tags

08/17/08 08:52:38 changed by idangazit

  • has_patch set to 1.

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.

08/17/08 10:48:04 changed by mtredinnick

  • stage changed from Accepted to Ready for checkin.
  • milestone set to 1.0.

08/23/08 13:43:01 changed by mtredinnick

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

08/24/08 10:51:40 changed by idangazit

  • attachment i18n_r8520_after_docsrefactor.patch added.

Same patch, applied to trunk post-docs-refactor

08/24/08 10:53:03 changed by idangazit

Updated documentation patch to work following docs-refactor in r8506

08/25/08 14:28:42 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #5591 (i18n documentation: Incorrect statement about template variables in {% trans %})




Change Properties
Action