Templates: {% trans %} {% some_tag _() %} and {{ _() }} what gives?

5 views
Skip to first unread message

Dmitri Fedortchenko

unread,
Nov 7, 2007, 8:01:35 AM11/7/07
to Django developers
I am trying to figure out the state of the translation tags in the
django template system.

My ticket has been rejected, but I am not satisfied with the RTFM
answer given there, based on the claim that python-stuff should not
work in the template system.

So why then do the i18n docs state that {% some_user_defined_tag
_("some text") %} is valid syntax.

It also seems that {{ _("some text") }} is valid syntax, however this
is not explicitly documented.

Why is this important?

Because most translations in my models.py file are lower case, since
the django admin automatically applies the python string's title()
method to them when needed. Great idea.
So I obviously am not interested in creating duplicate translations
for all my model verbose_names just to have them appear in my
templates. Right?

What I want is to do this:

{{ _("keywords")|title }}

And it works (only with "double", not 'single' quotes)!!!

I also tried creating a title tag that would title() it's first
parameter like this:

{% title _("keywords") %}

And it did not work using the simple_tag decorator so I submitted a
patch to make it work (based on the current docs saying that it
should): http://code.djangoproject.com/ticket/5885

I have also done this:

{% trans "keywords" title %}

And it obviously didn't work, so I wrote a patch to make it work.

Soon after I wrote this patch I found through this mailing list that
the _() syntax works in the templates now, so I decided not to write
any more code since it was already written and deleted said patch.

However I wanted to make sure that the documentation was more clear on
the subject and also more clear on the fact that the _() syntax in the
templates only works with "double quotes" and not 'single quotes'. I
wanted this documented or fixed because it is not consistent with the
rest of the template system.

According to my ticket http://code.djangoproject.com/ticket/5886 it
seems that the _() syntax in the templates is invalid, but the i18n
documentation says otherwise...

Django is full of stuff that is not documented and not deprecated but
works nonetheless. In fact django's creed is usually check the docs
then check the source, no?

Which one of these alternatives is correct? Which one of these
alternatives is not deprecated? And what patch is needed to sort out
this mess?

Please don't tell me that I have to write some block tag like this:
{% title %}{% trans "keywords" %}{% endtitle %}, it seems highly
counterproductive and not very friendly to my fingers.
I think that if the _() syntax is truly deprecated, then the {% trans
"hi" title %} method might be the best. Or perhaps to add the ability
to apply filters to the text like this: {% trans "some text"|title %}

Help me Scoobi-Doob Benoobi, you're my only hope.

//Dmitri

Malcolm Tredinnick

unread,
Nov 7, 2007, 6:00:33 PM11/7/07
to django-d...@googlegroups.com
On Wed, 2007-11-07 at 13:01 +0000, Dmitri Fedortchenko wrote:
> I am trying to figure out the state of the translation tags in the
> django template system.
>
> My ticket has been rejected, but I am not satisfied with the RTFM
> answer given there, based on the claim that python-stuff should not
> work in the template system.

Using Python as your guide for what should work in the template system
is mistaken. In that respect the ticket responders are correct. The
template system is not Python. The fact that it happens to use some
characters like _() and ".." that are also used in Python is
coincidence. You do have to understand that to make things easier.

As for the rest, I think there's just been some confusion in the ticket
between what accidentally works and whether it's a good idea or not.

It is fully intended that _() works in block tags. As an unintended
side-effect it also works in variable resolution tags. Okay, it's
probably not a bad thing. Send in a documentation patch for that.

Note, however, as was discovered on django-users recently, there is
actually a bug in the way translated portions are parsed in the template
variable parser. I'll fix that when I get a minute.

The double quotes versus single quotes issue just isn't the big deal
you're making it out to be. Okay, we allow single quotes for string
literals in templates, but it seems that everybody tends to use double
quotes and so they're what get tested mostly. Just use double quotes and
you'll be fine. The above fix to make _() work reliably will probably
fix this, but it's really not worth repeating over and over in your bug
report and the mail you send in.

Malcolm

--
The sooner you fall behind, the more time you'll have to catch up.
http://www.pointy-stick.com/blog/

Dmitri Fedortchenko

unread,
Nov 7, 2007, 6:06:58 PM11/7/07
to django-d...@googlegroups.com
I guess I got a little too excited over this ;-)

 
As for the variable resolution bug, could it be related to this*:

or am I on the wrong track?

//D
* shameless plug
Reply all
Reply to author
Forward
0 new messages