Search code examples
djangointernationalizationdjango-templatesdjango-i18n

How to specify context of translation in Django {% trans %} {% blocktrans %}?


Documentation of Django says Contextual markers are also supported by the trans and blocktrans template tags. but it not explained how to do it?

Can you help marking translation context since I have some words with several meanings.

In Python I can do in such way:

pgettext("month name", "May")
pgettext("verb", "May")

How to specify translation context in Django template?

{% blocktrans %}May{% endblocktrans %}

Solution

  • It is explained at the very end of their specific paragraphs:

    https://docs.djangoproject.com/en/dev/topics/i18n/translation/#trans-template-tag

    {% trans %} also supports contextual markers using the context keyword:

    {% trans "May" context "month name" %}
    

    https://docs.djangoproject.com/en/dev/topics/i18n/translation/#blocktrans-template-tag

    {% blocktrans %} also supports contextual markers using the context keyword:

    {% blocktrans with name=user.username context "greeting" %}Hi {{ name }}{% endblocktrans %}