Reputation: 6384
I am using django i18n for supporting i18n. I've found out that in django blocktrans an object, dict directly doesn't work.
For example if I've got a object with name obj and i try using it like
{% blocktrans %} My name is {{ obj.name }} {% endblocktrans %}
will not work, but if I use it like
{% blocktrans with name=obj.name %} My name is {{ name }} {% endblocktrans %}
will work.
I just wish to know why first example didn't work but second worked.
Upvotes: 4
Views: 1087
Reputation: 77912
Django's blocktrans
are passed to ugettext, which mark them as translation strings in the u"My name is %(name)s" form, which at runtime are processed with the context as mapping, ie `u"My name is %(name)s" % context. This does not allow for Django template style attribute resolution.
Upvotes: 4
Reputation: 25589
From the Django documentation "To translate a template expression -- say, accessing object attributes or using template filters -- you need to bind the expression to a local variable for use within the translation block"
Without delving into the template code I would guess that the translation operation is performed before the getattr/automatic calling stuff that django does when rendering a template.
Upvotes: 1