Django template: enable translation with variable

I have a template in which you can pass a text variable. I want to include this template in another, but with the translation of the text as a variable. How can you achieve this?

I would like something like this:

{% include "a_dir/stuff.html" with text={% trans "Load more promotions" %} %} 

I find it difficult to write my own template tag that will execute the ugettext , but then when creating the .po file, the text variable will not be taken automatically.

I do not want to do this work in view , since all our translations take place in templates.

+3
source share
1 answer

You can put the translated string in a variable with as syntax. For instance:

 {% trans "Load more promotions" as promotions %} {% include "a_dir/stuff.html" with text=promotions %} 

See documents for more details.

+3
source

Source: https://habr.com/ru/post/959053/