Paritosh Singh
Paritosh Singh

Reputation: 6384

Why in i18n blocktrans (django) a object, dict or list don't work?

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

Answers (2)

bruno desthuilliers
bruno desthuilliers

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

aychedee
aychedee

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

Related Questions