Eduardo
Eduardo

Reputation: 258

How can I add a translation template-tag inside another template-tag in Django?

This is my template code:

{{ can_edit|yesno:'Allow edit,View Only' }}

But I want to translate it automatically from my translation strings, so I did this:

{{ can_edit|yesno:'{% trans "option_allow_edit" %},{% trans "option_allow_edit" %}' }}

But it doesn't work, because it escapes the {% trans %} tags.

How can I do it?

Upvotes: 2

Views: 1171

Answers (2)

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

You can use the _() syntax.

Here is an example from the Django documentation:

{% some_tag _("Page not found") value|yesno:_("yes,no") %

So in your case you can do this:

{{ can_edit|yesno:_('Allow edit,View Only') }}

Upvotes: 2

schillingt
schillingt

Reputation: 13731

You should try using the blocktrans template tag.

{% blocktrans with editable=can_edit|yesno:'Allow edit,View Only' %}
    {{ editable }}
{% endblocktrans %}

Upvotes: 1

Related Questions