34

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.

beddamadre
  • 1,583
  • 2
  • 19
  • 40

2 Answers2

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.

Michal Čihař
  • 9,799
  • 6
  • 49
  • 87
  • 6
    Yes, but pgettext is not only about providing information to translators, it also differentiates the message for gettext and make them different messages. – Michal Čihař Apr 27 '12 at 05:43
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>
Diego Bianchi
  • 601
  • 5
  • 10