Django i18n +模板包含标签

tze*_*man 3 django django-templates django-i18n

我需要呈现翻译的推荐书,我使用包含块作为推荐代码,如下所示:

{% include "includes/blog/testimonial.html" with text="This is the best product I've ever used!" name="Tim Z" description="Store Manager" %}
Run Code Online (Sandbox Code Playgroud)

我如何blocktransinclude标签一起使用来呈现翻译的包含标签?

谢谢!!

Lud*_*mer 7

如果我理解正确,您希望将您提供的文本翻译为"包含"标签.如果这是正确的,只需事先翻译它并将结果保存到变量:

{% trans "This is the best product I've ever used!" as text %}
{% trans "Store Manager" as description %}
{% include "includes/blog/testimonial.html" with text=text name="Tim Z" description=description %}
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,包含模板可以访问主要上下文,所以在这种情况下你可以写`{%include'includes/blog/testimonial.html"with name ="Tim Z"%}`,但我想说明一个更大的点. (2认同)