Zopper
Zopper

Reputation: 132

Django - custom translation (.mo file) strings doesn't work

I tried to set up translation for Django. Built-in Django strings are correctly translated (e.g., in /admin/), so language selection works, but I can't get it to use the custom compiled .mo file.

I went step by step according this answer, and some others, but without success - it looks like I have everything like in that one.

To points out things I checked up:

settings.py:

LOCALE_PATHS = (
    os.path.join(BASE_DIR,'locale'),
)
LANGUAGES = (
    ('en', _('English')),
    ('cz', _('Czech')),
)
...

MIDDLEWARE_CLASSES = [
    'thermostat.middleware.thermostatMiddleware.ForceDefaultLanguageMiddleware',
    'django.middleware.security.SecurityMiddleware',
    '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',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.i18n',
            ],
        },
    },
]

...
LANGUAGE_CODE = 'cz'
USE_I18N = True
USE_L10N = True
USE_TZ = True
...

(The ForceDefaultLanguageMiddleware only removes HTTP_ACCEPT_LANGUAGE header from request.META.)

In code, I'm using ugettext_lazy, and the .po file is generated as expected with django-admin makemessages -a. I translate it, run django-admin compilemessages, which prints processing file django.po in /[path to project]/locale/cz/LC_MESSAGES - the path is correct, the .po file really exists there.

For example, I marked a model for translation like this:

class Probe(models.Model):
    class Meta:
        verbose_name = _('Probe')
        verbose_name_plural = _('Probes')
    name = models.CharField(_('Name'), max_length=200)

And this is where I ends - no matter what I tried, Django translates only is built-in messages and ignores custom strings. So in administration, I have translated all the buttons, but not the field and model names. Any ideas what I do wrong?

Upvotes: 0

Views: 1248

Answers (1)

Zopper
Zopper

Reputation: 132

OK, so as I was just about to send the question, I found the answer.

I was using LANGUAGE_CODE = 'cz', and Django took it as an alias to 'cs' (this is ok), but for some reason, it didn't load the custom .mo file. When I changed the language code to cs as it is in django/conf/locale/ path, it began to work.

I do not understand why this happens, but at least it works.

Upvotes: 1

Related Questions