问题
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