2

目前我正在将 Flask 和 Jinja2 与 Babel 结合使用。到目前为止,一切都很好。

我面临的唯一问题是在翻译文本中使用链接时。假设我们在 HTML 中有以下内容:

<p>You can change this in your <a href="{{ url_for("settings") }}">settings</a>.</p>

我将如何将它与 babel 结合使用?

我一直在考虑以下代码,但是当翻译语言中的单词顺序不同时,这会出现问题。

<p>{{ _("You can change this in your ")<a href="{{ url_for("settings") }}">{{ _("settings")</a>.</p>

另一个想法是将整个 HTML 放入要翻译的字符串中,但是我不能再使用字符串转义了。

这样做的首选方法是什么?

4

1 回答 1

2

我正在寻找一个解决方案,并遇到了这个优秀的页面

您可以使用可用于 jinja2 变量的字符串替换来添加链接。这是一个例子:

{{ _("You can change this in your %(open)ssettings%(close)s.", open='<a href="/user/%d">' % user_id, close='</a>')|safe }}

然后,您的 .po 文件将包含如下条目:

msgid "You can change this in your %(open)ssettings%(close)s."
msgstr ""
于 2017-01-22T17:17:37.280 回答