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

邮差的信 提交于 2020-12-13 03:45:29

问题


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?


回答1:


You should try using the blocktrans template tag.

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



回答2:


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') }}


来源:https://stackoverflow.com/questions/21740956/how-can-i-add-a-translation-template-tag-inside-another-template-tag-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!