Gab
Gab

Reputation: 5784

Django template: Translate include with variable

I have a template in which you can pass a text variable. I want to include this template into another one but with a translated text as it's variable. How can you achieve this?

I would like something like this:

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

I tough about writing my own template tag that will perform a ugettext but then when creating the .po file, the text variable will not be taken automatically.

I don't want to do this work in the view since all our translations take place in the templates.

Upvotes: 7

Views: 2880

Answers (2)

Seb D.
Seb D.

Reputation: 5195

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

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

See the docs for more details.

Upvotes: 8

ProfHase85
ProfHase85

Reputation: 12183

A shorter way is

{% include 'a_dir/stuff.html' with text=_("Load more promotions") %}

which also works fine with variables

Upvotes: 3

Related Questions