I've got an issue with translations not working on Django 1.6. I've added this to my settings.py
:
LANGUAGE_CODE = 'en-us'
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('English')),
('de', ugettext('German')),
)
Also added middlewares:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
As well as to my *.py files whenever I'm using a string which shall be l10nd:
from django.utils.translation import ugettext_lazy as _
My templates start with:
{% extends "base.html" %}
{% load i18n %}
And inside the template, I used the trans
placeholder. E.g.
<h1>{% trans "Register a tank" %}</h1>
I have provided translations in locale/de/LC_MESSAGES/django.po
:
msgid "Register a tank"
msgstr "Einen neuen Tank anmelden"
My browser is set to request German content first: Browser settings
What did I miss?
P.S. The project I'm currently fuzzy around is hosted on GitHub: https://github.com/frlan/blankspot
Add
LOCALE_PATHS
tosettings.py
and set it as below:Note that
LOCALE_PATHS
must be a tuple (look at the comma at the end of the path).Now based on
LOCALE_PATHS
, thelocale
folder should be in the root of your project.And be sure that you run the commands
django-admin.py makemessages -l de
anddjango-admin.py compilemessages
from the root of your project.Also rearrange your
MIDDLEWARE_CLASSES
to beLocaleMiddleware
afterSessionMiddleware
and beforeCommonMiddleware
as mentioned here:Restart your service (
python manage.py runserver
) and check again.Just to ensure that your localization is applied to your Django admin page with the default
django.mo
file of Django, do the following test:First in main
urls.py
of project replacepatterns
withi18n_patterns
:Now go to the admin page with a
de
prefix, like:http://127.0.0.1:8000/de/admin/
And the admin page should be shown in German.OK, are you able to see the admin page of Django in German?
Also check your view with the
de
prefix too.According to your project code, some sentences are not in
trans
blocks. Put them as:Also you must use
ugettext_lazy
instead ofugettext
in your code for views and models (Read here and here.)Replace this:
with:
And now everything will work.