[Django]-Same string with different translation

36👍

You can use gettext’s context for that. Django has added support for that in 1.3 release (in code) and 1.4 (for templates), see https://docs.djangoproject.com/en/dev/topics/i18n/translation/#contextual-markers

Update:

For example following code:

from django.utils.translation import pgettext, ugettext

month = pgettext("month name", "May")
month = pgettext("fifth month", "May")
month = ugettext("May")

Translates to:

#: foo/views.py:4
msgctxt "month name"
msgid "May"
msgstr ""

#: foo/views.py:5
msgctxt "fifth month"
msgid "May"
msgstr ""

#: foo/views.py:6
msgid "May"
msgstr ""

Each message being different and can be translated differently.

0👍

In case you need also to pass the variable to your front-end

views.py

from django.utils.translation import pgettext
from django.shortcuts import render

def index(request):

our_masculine = pgettext("masculine", "Our")
our_feminine = pgettext("feminine", "Our")

return render(request,'index.html', {'our_masculine': our_masculine,'our_feminine': our_feminine })

django.po

#: index/views.py:16
msgctxt "masculine"
msgid "Our"
msgstr "I nostri"

#: index/views.py:17 
msgctxt "feminine"
msgid "Our"
msgstr "Le nostre"

index.html

<h4>{{ our_masculine }}</h4>

Leave a comment