11

假设我有一个 Jinja2 模板,并且我正在使用 Flask-Babel 来翻译我的项目。例如:

<p>The <em>best</em> way of using the Internet is
to use <a href="{{ url_for('our_site') }}">our site</a>.</p>

所以我有一个带有链接和强调的句子。假设我想翻译我的句子。显而易见的方法是使用gettext(){% trans %}标记:

<p>{% trans %}The {% endtrans %} <em>{% trans %}best{% endtrans %}</em>
{% trans %}way of using the Internet is to use{% endtrans %}
<a href="{{ url_for('our_site') }}">{% trans %}our site{% endtrans %}</a>{% trans %}.{% endtrans %}</p>

显然,问题在于这会将句子分解成多个不能很好翻译的片段。这将导致翻译人员将字符串“The”、“best”、“way of using the Internet is to use”和“our site”视为所有单独的字符串,加上标点符号。当然,翻译者会想要重新构造句子,并选择单独连接和强调的单词。

那么鉴于此,解决方案是什么?如何将一个带有标签的句子翻译为一个单元?

4

2 回答 2

3

您可以使用 gettext() 和安全过滤器

{{ gettext('The <em>best</em> solution') | safe }}

http://jinja.pocoo.org/docs/2.9/templates/#list-of-builtin-filters

然后,您的翻译人员将能够安排标签。

如果您想让翻译人员的事情变得更简单,您可以添加一个自定义降价过滤器并使用它在短语中添加简单格式,请参阅此处以获取示例https://gist.github.com/globard/7554134

于 2017-02-03T14:14:20.767 回答
3

你可以这样做:

{% trans url=url_for('our_site') %}
<p>The <em>best</em> way of using the Internet is to use <a href="{{ url }}">our site</a>.</p>
{% endtrans %}

与对象嵌套变量 ( obj.site_name) 相同:

{% trans url=url_for('our_site'), site_name=obj.site_name %}
<p>The <em>best</em> way of using the Internet is
to use <a href="{{ url }}">our site</a>{{ site_name }}.</p>
{% endtrans %}

所以你必须将变量声明为trans函数参数,否则trans将不起作用。要了解更多信息,请访问docs

于 2020-11-05T20:29:17.607 回答