6

I'm using Django 1.6, and I feel like I'm missing something, but cookies are set to current language chosen, but the display language stays the default.

Corresponding code:

settings.py

LANGUAGES = (
    ('hu', 'Hungarian'),
    ('en', 'English'),
)

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request"
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware'
)
LANGUAGE_CODE = 'en-US'
TIME_ZONE = 'CET'
USE_I18N = True
USE_L10N = True
USE_TZ = True

urls.py

urlpatterns = patterns('',
    url(r'^i18n/', include('django.conf.urls.i18n')),
    ...
)

template

{% extends 'base.html' %}
{% load i18n %}
...
<h4>{% trans "Modern Technologies" %}</h4>
...

I ran makemessages -a to create the lang files, rosetta is installed and languages are edited. Then I've ran compilemessages. Checking in Chrome the cookie "django_language" get's set correctly. But the actual text is still the default "Modern Technologies".

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Tusk
  • 703
  • 11
  • 21

3 Answers3

10

Your middleware order is different from that recommended by the documentation:

To use LocaleMiddleware, add 'django.middleware.locale.LocaleMiddleware' to your MIDDLEWARE_CLASSES setting. Because middleware order matters, you should follow these guidelines:

  • Make sure it’s one of the first middlewares installed.
  • It should come after SessionMiddleware, because LocaleMiddleware makes use of session data. It should also come before CommonMiddleware because CommonMiddleware needs an activated language in order to resolve the requested URL.
  • If you use CacheMiddleware, put LocaleMiddleware after it.

So your middleware configuration should look like this:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

You also need to remember to include the LOCALE_PATHS setting in your settings file:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
Community
  • 1
  • 1
Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
  • 1
    I've changed up the order to match this, but still it does not change the displayed language :( – Tusk Feb 04 '14 at 13:25
  • 1. Have you tried setting the language again, after you changed the order? 2. Do you use Django 1.6.1 or Django 1.6.0? – Ludwik Trammer Feb 04 '14 at 13:32
  • 1. Sure. I even changed the LANGUAGE_CODE to "hu" and that won't change a thing. 2. 1.6.1 – Tusk Feb 04 '14 at 13:40
  • 1. Did you changed it using the built-in *set_language* view or using some other method (for example directly setting a cookie)?; 2. Are you sure your translation files are discovered and loaded correctly? Try displaying the current language directly, for example using `{% get_current_language as LANGUAGE_CODE %}`. – Ludwik Trammer Feb 04 '14 at 14:25
  • 1, changing it with the form found at: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#the-set-language-redirect-view after refreshing the page it keeps the chosen selected language so that should work. 2. That changes nothing. I have them in a locale folder in root where manage.py is. Tried putting it in conf/locale but doesn't change a thing. (since rosetta founds it I don't that is the problem) – Tusk Feb 05 '14 at 09:47
  • The second recommendation wasn't suppose to change anything - just display current language code, so you can verify which language is actually turned on. – Ludwik Trammer Feb 05 '14 at 12:52
  • You wrote that after changing the language, the new language is kept selected. This clearly means language selection works, and Django just can not find your translation files. Do you have the `LOCALE_PATHS` setting specified? – Ludwik Trammer Feb 05 '14 at 14:28
2

Okay, the problem was, that I've put my locale folder into root, and it expects it inside an installed app.

However rosetta finds it even if you have the translation outside an app.

Tusk
  • 703
  • 11
  • 21
1

Try insert 'django.middleware.locale.LocaleMiddleware', between SessionMiddleware and CommonMiddleware.

As it makes in docs. If I'm not mistaken, it's important.

Dmit3Y
  • 496
  • 2
  • 5