问题描述
我正在使用 jinja 2.10 和 pybabel.当我的模板包含以下代码(在 trans 块中带有 '%' 字符)时,pybabel-compile 不会翻译字符串.提取的字符串(在 .po 中)是可以的,但在结果页面上它根本没有被翻译.
I am using jinja 2.10 and pybabel. When my template contains following code (with '%' char inside trans block) pybabel-compile does not translate the string. The extracted string (in .po) is OK but on the result page it is not translated at all.
<h3 class="title">{% trans %}100% anonymity{% endtrans %}</h3>
此代码有效,但反式语法看起来更好:
This code works but the trans-syntax looks better:
<h3 class="title">{{ _("100%% anonymity") }}</h3>
另外,我也懒得用%%"代替%"来打扰我的翻译同事.
Also I cannot my bother my translating colleagues with using '%%' instead of '%'.
推荐答案
我也遇到过这个问题,但我在 Babel 中找到了 文档 解决办法:
I had stuck about this too, but I found in Babel documentation the solution:
##flask.ext.babel.gettext(string, **variables)##
##Translates a string with the current locale and passes in the given keyword arguments as mapping to a string formatting string.
gettext(u'Hello World!')
gettext(u'Hello %(name)s!', name='World')
我翻译成这样的简单代码:
And I translate to simple code like this:
_('Hello %(name)s!', name='World%')
希望能帮到你 :D
这篇关于如何使用 pybabel 在 jinja 2.10 {% trans %} 中转义“%"字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!