Django模板:使用变量翻译包含 [英] Django template: Translate include with variable

查看:317
本文介绍了Django模板:使用变量翻译包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板可以传递一个文本变量。我想将此模板包含在另一个模板中,但包含一个翻译的文本,因为它是可变的。如何实现这一点?



我想要这样:

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

我很难写出自己的模板标签,它将执行 ugettext ,但是当创建 .po 文件,文本变量将不会被自动采用。



我不想在视图中执行此工作因为我们的所有翻译都发生在模板中。

解决方案

您可以将翻译的字符串放入变量中, 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.

这篇关于Django模板:使用变量翻译包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆