Amandasaurus
Amandasaurus

Reputation: 60799

Django template translation - Is it possible to use object attributes

Django's {% blocktrans %} template tag doesn't support object attribute accesses, nor template filters. Is there any django library that will allow me to use object attributes and filters inside a blocktrans-type-tag?

Essentially I want to be able to write:

{% load ??? %}
{% blocktrans %}The price is €{{ object.price }}.{% endblocktrans %}

I know the standard Django advice is to use variables on the blocktrans, but I want to avoid that. It results in a much larger template and involves much more typing and template rewriting in order to i18n-ify your django app.

Before I go and write this library myself, I want to see if anyone has done it yet.

Upvotes: 1

Views: 171

Answers (1)

Gocht
Gocht

Reputation: 10256

You can try with {% with %}:

{% with object.price as myprice %}
{% blocktrans %}The price is € {{ myprice }}.{% endblocktrans %}

Upvotes: 1

Related Questions