In our examples we used the LANGUAGE_CODE
setting to choose the default language for our site. Let's configure our project to automatically determine the user's language preference from their browser.
We can detect the user's language preference by inspecting the Accept-Language
HTTP header. Luckily, Django makes it very simple to inspect and take action based on the header.
In your mycompany/settings.py
file, add a special piece of middleware called LocaleMiddleware
to your MIDDLEWARE_CLASSES
tuple by adding the highlighted line:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
If the classes listed in your project are slightly different, that's OK. Just make sure you add LocaleMiddleware
after the SessionMiddleware
entry. The order is important because Django...