Search code examples
djangointernationalizationtranslation

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.


Solution

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