Django translations does not work

jeff picture jeff · Apr 17, 2015 · Viewed 10.9k times · Source

I'm trying to get Django's translation system to work, following the tutorial here.

Here are my two views (one for direct output, one for template), neither one works.

def home(request):        
    output = _("hello") # (lazy)
    return HttpResponse(output)

def with_template(request):
    return render(request, 'translation_template.html')

here is the template file for the second view :

{% extends "base_site.html" %}
{% load i18n %}

{% block content %}
<p>{% trans 'hello' %}</p>
{% language 'tr' %}
<p>{% trans 'hello' %}</p>
{% endlanguage %}
{% language 'tr-TR' %}
<p>{% trans 'hello' %}</p>
{% endlanguage %}
{% endblock %}

in my settings file, I added the following: (may add parts from before if requested)

LANGUAGE_CODE = 'en-us'    
# also tried LANGUAGE_CODE = 'tr'  and LANGUAGE_CODE = 'tr-TR'
PROJECT_DIR = os.path.dirname(__file__)

"""
# tried but didn't work
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.context_processors.auth",
    "django.core.context_processors.i18n",
)
"""
LOCALE_PATHS = ( os.path.join(PROJECT_DIR, 'locale'), )
LANGUAGES = (
    ('tr', _('Turkish')),
    ('en', _('English')),
)

after saving these, I executed in the terminal :

python ./manage.py makemessages -l tr

then edited the newly created myproject/locale/tr/LC_MESSAGES/django.po to have this :

msgid "hello"
msgstr "merhaba"

then executed

python ./manage.py compilemessages

and restarted the server. the terminal commands show no error, but when I load the views, none of the "hello"s are translated.

What am I doing wrong here?

Thanks for any help!

Edit:

I found a suspicious code in en/../django.po, probably not relevant, but maybe it is. This is the very beginning of the file. The fuzzy (empty->empty) translation, could it be the problem?

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""

Answer

jeff picture jeff · Apr 17, 2015

I solved my issue. In my case, the problem was with the LOCALE_PATHS definition in settings.py.

I tested it in the view by :

from TranslationTest import settings
return HttpResponse(settings.LOCALE_PATHS)

It was showing home/myProjects/TranslationTest/TranslationTest/locale, however makemessages was producing the files in home/myProjects/TranslationTest/locale

so I changed my settings as follows :

SITE_ROOT = os.path.dirname(os.path.realpath(__name__))
LOCALE_PATHS = ( os.path.join(SITE_ROOT, 'locale'), )

and now it works.

But I still wonder, why didn't makemessages understand that it should create the files in the LOCALE_PATHS specificed by settings.py?

In my understanding, it always produces the locale files in SITE_ROOT/locale, so we should always set LOCALE_PATHS to this? If this is a default, why set it at all? I would appreciate further information on this issue.

Thanks for all the help !