0

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?

Mateusz Knapczyk
  • 276
  • 1
  • 2
  • 15
  • Have you marked your strings for translation? I'm asking because I see `value="Zmień język"` in your template – Andrea Corbellini Oct 09 '15 at 12:13
  • Everything except that is marked, .po file has translations, I leave "Zmień język" because later I'm going to change that select into 'click-on-flag', nice clickable module :) – Mateusz Knapczyk Oct 09 '15 at 12:15
  • Mh... for what you posted it appears you just have `locale/en/LC_MESSAGES`. How about other languages? Have you translated your strings? – Andrea Corbellini Oct 09 '15 at 12:19
  • In my views and HTMLs I have (marked for translation) strings in polish. I've run makemessages, edited django.po, and run compilemessages. Should I have dir for 'pl'? With what, empty django.po? – Mateusz Knapczyk Oct 09 '15 at 12:21
  • Translations generally does not work that way. Your solution is somewhat hackish. In general, in your Python code and templates, you should have English strings. In your `en/LC_MESSGAGES/django.po` you should have `msgstr`s equal to `msgid`s. Then you should have a `pl/LC_MESSGAGES/django.po` with your English `msgid`s and Polish `msgstr`s. – Andrea Corbellini Oct 09 '15 at 12:26
  • You could also keep your Polish `msgid`s, but you should provide a `pl/LC_MESSGAGES/django.po` file. – Andrea Corbellini Oct 09 '15 at 12:27
  • So, I could have polish strings in code and templates, make `pl/LC_MESSGAGES/django.po` with equal `msgst`r and `msgid`, and then make `en/LC_MESSGAGES/django.po` with translations, right? But, in the other hand, I got my `LANGUAGE_CODE` changed to "en", and still nothing – Mateusz Knapczyk Oct 09 '15 at 12:30

1 Answers1

0

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

Mateusz Knapczyk
  • 276
  • 1
  • 2
  • 15