Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 323
boatcoder Avatar asked Aug 19 '10 01:08

boatcoder


1 Answers

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

like image 75
Wolph Avatar answered Nov 12 '22 02:11

Wolph