boatcoder
boatcoder

Reputation: 18117

How do I translate the output of a filter in Django

I have some template code that looks like:

<input type='submit' value='{{ need.satisfied|yesno:"Resend this document now,Send this document now" }}' />

I'd like to be able to translate it but that appears to be difficult to accomplish.

http://code.djangoproject.com/ticket/3804 mentions

{{ _("Some String") }} 

which appears to work for literal strings, but when used like

{{ _(Variable) }} 

gives the error

Variables and attributes may not begin with underscores: '_'

So, how do you do it?

Oh yea, I tried doing:

'{% if blah %}{% trans "Resend..." %}{% else %}{% trans "Send..." %}{% endif %}'

which works, but look so ugly I don't want to. Surely with Django there is some more elegant way to do this.....

Seems like a |trans filter would be in order, but that was shot down as a non-issue with http://code.djangoproject.com/ticket/3804

Upvotes: 7

Views: 3523

Answers (2)

Wolph
Wolph

Reputation: 80061

Judging by the example in the docs, it might be possible to do it like this:

<input type='submit' value='{{ need.satisfied|yesno:_("Resend this document now,Send this document now") }}' />

Source: https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#string-literals-passed-to-tags-and-filters

Upvotes: 17

Ashok
Ashok

Reputation: 10603

have you tried using, blocktrans

{% blocktrans %}
    string to translate with {{ vars }}
{% endblocktrans %}

Upvotes: 1

Related Questions