I've got an issue with translations not working on Django 1.6!. I've added 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 starting with
{% extends "base.html" %}
{% load i18n %}
and inside the template than I used the trans placeholder. E.g.
<h1>{% trans "Register a tank" %}</h1>
In fact the string is appearing inside locale/de/LC_MESSAGES/django.po when running as well I have translated them. E.g.
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 fuzzying around is hosted on github: https://github.com/frlan/blankspot
Please set
translated string
indjango.po
and then usepython manage.py compilemessages
Suggestion-: You can use
django-rosetta
package to addtranslated string
from UI interface. It is easy to add T-string from django-admin. https://github.com/mbi/django-rosettaYou need to enable the
LocaleMiddleware
in your settings, to tell Django to do language detection based on the browser-settings. Changing your language preferences effectly sets theAccept-Language
header. You might need to check in an incognito window, because other means of language detection have a higher priority, such as the user's session and thedjango_language
cookie.Add the
LOCALE_PATHS
tosettings.py
and set it as bellow: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 root of your project.And be sure that you run
django-admin.py makemessages -l de
anddjango-admin.py compilemessages
cmds from the root of your project.Also rearrange your
MIDDLEWARE_CLASSES
to beLocaleMiddleware
afterSessionMiddleware
and beforeCommonMiddleware
that 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 default
django.mo
file of django, do following test:First in main
urls.py
of project replacepatterns
withi18n_patterns
:Now go to
Admin
page withde
prefix, like:http://127.0.0.1:8000/de/admin/
AndAdmin
page should be show inGerman
language.Ok, are you able to see
Admin
page of django inde
language ?Also check your view with
de
prefix too.According to your code of project, some sentences are not in trans block. put them as:
Also you must use
ugettext_lazy
instead of ugettext in your code for views and models, (read here)replace this:
with:
And now everything will work.
In my case, I used en-gb as the parameter to run
django-admin.py makemessages -l en-gb
Instead, it should be en_GB.
django-admin.py makemessages -l en_GB
Check the cookies and the session -- according to How Django Discovers Language Preference, the process is this:
Since your browser settings are set to prefer 'de', I suspect the LocaleMiddleware must decide otherwise in one of the previous steps 1. - 3.