Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#13317 closed (fixed)

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

Reported by: Chris Morgan 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: no UI/UX: no


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 Morales 9 years ago.
Patch for trunk
13317-1.1.X-r13137.diff (5.0 KB) - added by Ramiro Morales 9 years ago.
patch for the 1.1.X branch

Download all attachments as: .zip

Change History (7)

comment:1 Changed 9 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

Changed 9 years ago by Ramiro Morales

Attachment: 13317-r13135.diff added

Patch for trunk

Changed 9 years ago by Ramiro Morales

Attachment: 13317-1.1.X-r13137.diff added

patch for the 1.1.X branch

comment:2 Changed 9 years ago by Ramiro Morales

milestone: 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 for an example): Contrast the functionality of trans and blocktrans, describe that blocktrans has two roles.

comment:3 Changed 9 years ago by Ramiro Morales

Has patch: set

comment:4 Changed 9 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

(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 8 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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