Django模板:使用变量翻译include

7

我有一个模板,可以传递文本变量。我想将此模板包含到另一个模板中,但使用翻译后的文本作为其变量。如何实现这一点?

我希望像这样:

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

我考虑编写自己的模板标签来执行 ugettext,但是当创建 .po 文件时,文本变量将不会被自动获取。我不想在 view 中完成这项工作,因为我们所有的翻译都发生在模板中。
2个回答

8

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

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

请参阅文档以获取更多详细信息。


2
更短的方式是:
{% include 'a_dir/stuff.html' with text=_("Load more promotions") %}

它也可以很好地处理变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接