Search code examples
pythondjangojinja2gettext

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 templates.

I want to use pgettext() (django docs) as well to add a context to the translatable strings. Is there an extension that does this, or am I missing something?

update: I have posted a feature request #793. I would still like to know whether there is an answer that can be used right now.


Solution

  • I don't believe that what you want exists in Jinja... (at least for now)
    If you take a look at this issue the 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 back in 2014 and something may have changed but I don't think so (based on another issue).

    By searching a bit around I found this project which may 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 :)