Django模板:翻译包含变量

Gab*_*Gab 4 python django templates

我有一个模板,您可以在其中传递文本变量.我希望将此模板包含在另一个模板中,但使用翻译后的文本作为变量.你怎么能实现这个目标?

我想要这样的东西:

{% include "a_dir/stuff.html" with text={% trans "Load more promotions" %} %}
Run Code Online (Sandbox Code Playgroud)

我很难写自己的模板标签,它会执行a ugettext但是在创建.po文件时,文本变量不会自动生成.

我不想这样做,view因为我们所有的翻译都发生在模板中.

Séb*_*rez 5

您可以使用as语法将已翻译的字符串放入变量中.例如:

{% trans "Load more promotions" as promotions %}
{% include "a_dir/stuff.html" with text=promotions %}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅文档.