且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django模板:使用变量翻译包含

更新时间:2023-10-25 17:40:04

您可以将翻译的字符串放入变量中, code> as 语法。例如:

  {%trans加载促销活动为促销%} 
{%includea_dir / stuff .htmlwith text = promotion%}

请参阅文档了解更多详情。


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.

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

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

See the docs for more details.