Python – pgettext in jinja2 django templates

pgettext in jinja2 django templates… here is a solution to the problem.

pgettext in jinja2 django templates

I have configured the Jinja environment with env.install_gettext_translations(translation, newstyle=True) and the jinja2.ext.i18n extension.
I can only use the _(), _gettext(), and ngettext() functions in my jinja2 template.

I also want to use pgettext() ( django docs ) to add context to translatable strings. Is there an extension to do this, or am I missing something?

Update: I’ve posted a feature request#793.

Solution

I don’t believe that what you want exists in Jinja … (at least for now).
If you look at this issue developer pretty much states that he won’t do it:

This would require patches as I’m not using pgettext myself.

Then he closed the issue.
This was created in 2014, and some things may have changed, but I don’t think so (based on another issue).

By searching around, I found this item that might help: https://pypi.python.org/pypi/jinja2-django-tags/0.1. There I read:

You can also use _, gettext and pgettext directly:

Simple example: {{ _('Hello World') }}
More verbose: {{ gettext('Hello World') }}
With context: {{ pgettext('Hello World', 'another example') }}

Good luck 🙂

Related Problems and Solutions