{% translate s %} 传入 {% include ... with s=s %} 不在 .po 文件中

{% translate s %} passed in {% include ... with s=s %} not in .po file

我有一个要在我的 Django 项目中呈现的基本提要。我创建了一个 feed.html 文件作为基本模板,用于相同类型提要的细微变化。在这些变体中,是提要的 header。重要的是,我希望 header 被翻译成多种语言。

我已经使用 {% include "feed.html" with variation=variation %} 实现了这个“变体”想法。但是,我在翻译这些变体时遇到了问题。

我正在 feed.html 中尝试以下操作:

{% translate header %}

然后在我想要 feed.html 变体的其中一个模板中,我有:

{% include "feed.html" with header="Header" %}

问题是,字符串“Header”没有进入我的任何 .po 文件,因此仍然无法翻译。

我做错了什么?我应该使用不同的语法吗?

The problem is, the string "Header" does not make it into any of my .po files and therefore remains untranslatable.

如果这意味着 makemessages 命令没有提取字符串,那么是的,它不会,因为没有字符串。只是一个变量。该变量可以取任何值,makemessages 不可能将其追溯到您可能为 header 设置值的所有可能位置。 makemessages 只能提取您 按字面意思 放入 {% translate %} 标签或直接 _() 函数调用的内容。

The caveat with using variables or computed values, as in the previous two examples, is that Django’s translation-string-detecting utility, django-admin makemessages, won’t be able to find these strings.

https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#standard-translation

您需要在传递 header 值之前对其进行翻译:

{% include "feed.html" with header=_("Header") %}