python - jinja2 django 模板中的 pgettext

标签 python django jinja2 gettext

我已经使用 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/

相关文章:

python - Django 小部件调整 'render_field' 不起作用

python - __enter__ 和 __exit__ 在 python 3 中的类级别

python - Pandas :选择带有unicode字符的字符串

python - Json 字段搜索与 django 意外行为

python - 检索当前模型之外的 Django ManyToMany 结果

javascript - Jquery/Ajax/CSS 在使用 Ajax 提交表单后显示/隐藏 div

templates - 如何获得 Ansible 模板以在条件后支持新行

python - pyside 嵌入 vim

python - 读取 Pandas 中以空格分隔的字符串数据

python - Django 中的模板标签