Search code examples
pythondjangolocaletranslationdjango-i18n

Yet another issue with Django Translation


After reading dozens of questions and answers about not working transtalions, I give up, because I did (I think I did) everything mentioned in other threads and still can't get work done.

My settings.py:

# -*- coding: utf-8 -*-

from __future__ import absolute_import
import os
from socket import gethostname

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
HOSTNAME = gethostname()

def make_path(path):
    return os.path.join(BASE_DIR, path)

def make_abs_path(path):
    return os.path.abspath(make_path(path))

def get_proj_root():
    curr_path = make_abs_path('')
    return os.path.split(curr_path)[0]

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = xxxxx

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

ADMINS = ()

MANAGERS = ADMINS

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',
    'rest_framework',
    # 'rest_framework.authtoken',
    'main'
)

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.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'Plan_Zajec.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'main.context.header_context',
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
        },
    },
]

ROOT_URLCONF = 'Plan_Zajec.urls'

WSGI_APPLICATION = 'Plan_Zajec.wsgi.application'

DATABASES = {
    'default': {xxxxx}
}

DEFAULT_CHARSET = 'utf-8'

TIME_ZONE = 'Europe/Warsaw'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_ROOT = make_path('static_root')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    make_path('main/static/main'),
)

LANGUAGES = (
    ('pl', 'Polski'),
    ('en', 'English'),
)

LANGUAGE_CODE = 'pl'

LOCALE_PATH = make_abs_path('locale/')

LOGIN_URL = '/'

# Celery conf:

BROKER_URL = 'redis://'
CELERY_RESULT_BACKEND = 'redis://'
CELERY_MAX_CACHED_RESULTS = 1000
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']

urls.py, on the end of urlspatterns:

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

Form in header (later it will be replaced with nice 'click-on-flag' select, but for now...)

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
    <input type="submit" value="Zmień język" />
</form>

Simply - I've done everything like the docs said. Django saves and remembers LANGUAGE_CODE (I can see that because of 'selected' if in form and I double checked that), but fails to translate anyway. LOCALE_PATH is valid:

>>> settings.LOCALE_PATH
'/home/mefioo/apps/generator/src/locale/'

I've run 'compilemessages':

locale/
└── en
    └── LC_MESSAGES
        ├── django.mo
        └── django.po

Any ideas?

EDIT: Order in MIDDLEWARE_CLASSES - after session, before common, still not working.

EDIT: After @Andrea Corbellini suggestion, I've made translations for 'pl' as well, msgstr equal to msgid, still nothing. How can I check if Django finds locale files, even if I'm sure LOCALE_PATH is correct?


Solution

  • Ok, I'm blind and my brain has tricked me, there is not such thing like LOCALE_PATH, there's LOCALE_PATHS. I added this litte 'S' and now everything works. (I need to go back to elementary school to learn how to read again, sorry for problems.)