【问题标题】:Django template: Translate include with variableDjango模板:翻译包含变量
【发布时间】:2014-07-18 18:40:41
【问题描述】:

我有一个模板,您可以在其中传递一个文本变量。我想将此模板包含到另一个模板中,但要使用翻译文本作为变量。你怎么能做到这一点?

我想要这样的东西:

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

我很难编写自己的模板标签来执行ugettext,但是在创建.po 文件时,不会自动获取文本变量。

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

【问题讨论】:

    标签: python django templates


    【解决方案1】:

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

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

    更多详情请见the docs

    【讨论】:

      【解决方案2】:

      更短的方法是

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

      这也适用于变量

      【讨论】:

        猜你喜欢
        • 2023-03-09
        • 1970-01-01
        • 2017-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        相关资源
        最近更新 更多