beddamadre
beddamadre

Reputation: 1603

Same string with different translation

I have one string that can be translated in varius part of my code in two different way.

Now if Use django-admin makemessages -l it

I get in django.po this:

#: pingapi/ping.py:17 pingapi/nots.py:10
msgid "may"
msgstr "maggio"

But I would want two different translation:

#: pingapi/ping.py:17 
msgid "may"
msgstr "posso"

#: pingapi/nots.py:10
msgid "may"
msgstr "maggio"

If I run django-admin compilemessage with the translation file posted up, I get:

Error: errors happened while running msgmerge
 error 'duplicate message definition' 

Any Hints? I'm using Django.

Upvotes: 34

Views: 6742

Answers (2)

Diego Bianchi
Diego Bianchi

Reputation: 742

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>

Upvotes: 0

Michal Čihař
Michal Čihař

Reputation: 10091

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.

Upvotes: 36

Related Questions