Django 模板:翻译包含变量

问题描述 投票:0回答:2

我有一个模板,您可以在其中传递文本变量。我想将此模板包含到另一个模板中,但带有翻译后的文本,因为它是可变的。你怎样才能做到这一点?

我想要这样的东西:

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

我很难编写自己的模板标签来执行

ugettext
,但是在创建
.po
文件时,文本变量将不会被自动获取。

我不想在

view
中完成这项工作,因为我们所有的翻译都在模板中进行。

python django templates
2个回答
8
投票

您可以使用

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

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

请参阅文档了解更多详细信息。


3
投票

更短的方法是

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

它也适用于变量

© www.soinside.com 2019 - 2024. All rights reserved.