Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#13317 closed (fixed)

{% blocktrans %}: accessing attributes and general documentation concerning variables

Reported by: ChrisMorgan Owned by: nobody
Component: Documentation Version: master
Severity: Keywords: blocktrans variables attributes
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In the Django template system, it's documented about using {% blocktrans %} with local variables; it says that:

"To translate a template expression -- say, using template filters -- you need to bind the expression to a local variable for use within the translation block:"

This caused me quite a bit of trouble as it didn't occur to me for a very long time that accessing attributes on a model object would qualify as a template expression. This meant that for ages I was trying to work out why this caused a KeyError:

{% blocktrans %}That will cost ${{ i.price }}{% endblocktrans %}

I think that it needs to be either specified in docs/topics/i18n/internationalization that this sort of a situation would need to be done like this:

{% blocktrans with i.price as price %}That will cost ${{ price }}{% endblocktrans %}

Or, better still, it should be fixed so that it works... but considering how gettext works this probably isn't reasonable. Currently though it only shows examples of filters and that made it so that the reason my code was breaking didn't occur to me.

Also the documentation of the "count" thing is very vague. It took me a while to work out (partly due to the fact that recognition "count" is missing from the Pygments lexer) that it is just using count instead of with or and, like the following. It could be considerably improved by spelling this out in so many words.

{% blocktrans with i.price as price count i.length as years %}That will cost ${{ price }} per year{% plural %}That will cost ${{ price }} per {{ years }} years{% endblocktrans %}

Attachments (2)

13317-r13135.diff (5.1 KB) - added by ramiro 5 years ago.
Patch for trunk
13317-1.1.X-r13137.diff (5.0 KB) - added by ramiro 5 years ago.
patch for the 1.1.X branch

Download all attachments as: .zip

Change History (7)

comment:1 Changed 5 years ago by russellm

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

Changed 5 years ago by ramiro

Patch for trunk

Changed 5 years ago by ramiro

patch for the 1.1.X branch

comment:2 Changed 5 years ago by ramiro

  • milestone set to 1.2

Ive attached patches for trunk and the 1.1.X that address the issues expressed by th OP and long standing pending enhancements to the documentation of the i18n template tags (see http://groups.google.com/group/Django-I18N/browse_frm/thread/bdcdaf433bfcd144 for an example): Contrast the functionality of trans and blocktrans, describe that blocktrans has two roles.

comment:3 Changed 5 years ago by ramiro

  • Has patch set

comment:4 Changed 5 years ago by jezdez

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

(In [13185]) [1.1.X] Fixed #13317 - Clarified documentation about how the blocktrans and trans template tags work with regard to variables. Thanks for the initial patch, Ramiro Morales.

Backport from trunk, r13184.

comment:5 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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