urls in blocktrans

145 views
Skip to first unread message

Trevor Caira

unread,
Dec 24, 2007, 12:17:15 AM12/24/07
to Django developers, doct...@gmail.com
Since assignment of variables is illegal in the template syntax, and
{% blocktrans %} cannot contain other tags (notably {% url %}), there
is a problem for marking strings as translatable which contain links
in them in django templates.

Sometimes you can get around it with something like {% blocktrans with
post.get_absolute_url as posturl %}See the <a
href="{{ posturl }}">next post</a>{% endblocktrans %} (sorry for the
contrived example)--but in the general case, you need a different
solution.

My perception is that the general workaround is to do the url
resolution with urlresolvers.reverse in the view and pass the url in
as template context. Of course, this is a disgusting hack, and I'd
like to come up with a workaround for this.

It appears to me that the only clean resolution will involve adding
new or changing existing template tags. Naturally the first
candidates to look at are {% blocktrans %} and {% url %}. Blocktrans's
syntax is a little strained as it is; so I think it makes sense to
augment {% url %} instead. One possible solution is:

{% blocktrans %}Baz foo <a href="{% url bar arg1,arg2 as barurl
%}">bar</a> bat.{% endblocktrans %}

which would generated the translatable string:

"Baz foo <a href="%(barurl)s">bar</a> bat."

What are your thoughts on this proposed solution?

-- Trevor

Malcolm Tredinnick

unread,
Dec 24, 2007, 3:34:11 AM12/24/07
to django-d...@googlegroups.com

There was a longish thread about this back in November. Changing the url
tag probably isn't the right solution here, since the problem is with
translations. We're pretty close to having a good syntax for blocktrans
changes come out of that thread, so I'd prefer work goes into
implementing the final solutions from there so that we can see how they
work out.

Malcolm

--
Many are called, few volunteer.
http://www.pointy-stick.com/blog/

Reply all
Reply to author
Forward
0 new messages