我已经使用 env.install_gettext_translations(translation, newstyle=True)
和 jinja2.ext.i18n
扩展配置了 Jinja 环境。
我只能在我的 jinja2 模板中使用 _()
、_gettext()
和 ngettext()
函数。
我还想使用 pgettext()
( django docs ) 为可翻译字符串添加上下文。是否有执行此操作的扩展程序,还是我遗漏了什么?
更新:我已经发布了一个功能请求#793 .我仍然想知道是否有可以立即使用的答案。
最佳答案
我不相信你想要的东西存在于 Jinja 中......(至少现在)
如果你看一下this issue开发人员几乎声明他不会这样做:
This would require patches as I'm not using pgettext myself.
然后他关闭了这个问题。
这是在 2014 年创建的,有些东西可能已经改变,但我不这么认为(基于 another issue )。
通过四处搜索,我找到了这个可能有帮助的项目:https://pypi.python.org/pypi/jinja2-django-tags/0.1 .在那里我读到:
You can also use _, gettext and pgettext directly:
Simple example: {{ _('Hello World') }} More verbose: {{ gettext('Hello World') }} With context: {{ pgettext('Hello World', 'another example') }}
祝你好运:)
关于python - jinja2 django 模板中的 pgettext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47998764/