This section describes how to install the localeurl application in your Django project.
Setup consists of installing the middleware and adding 'localeurl' to the installed applications list.
Add 'localeurl.middleware.LocaleURLMiddleware' to settings.MIDDLEWARE_CLASSES. It must come before 'django.middleware.common.CommonMiddleware' or settings.APPEND_SLASH will not work. Make sure Django’s built-in LocaleMiddleware is not in your MIDDLEWARE_CLASSES setting; LocaleURLMiddleware replaces it and the two will not work together. It must also come after django.contrib.sessions.middleware.SessionMiddleware if you plan using the session fallback (see LOCALEURL_USE_SESSION below).
Add 'localeurl' to settings.INSTALLED_APPS. Because the application needs to replace the standard urlresolvers.reverse function, it is important to place it at the top of the list:
INSTALLED_APPS = (
'localeurl',
...
)
If you want to use the view, include the localeurl URLconf module in your project:
urlpatterns = patterns('',
...
(r'^localeurl/', include('localeurl.urls')),
...
)
Make sure settings.LANGUAGE_CODE or its root language is in settings.LANGUAGES. For example, if LANGUAGE_CODE == 'en-us' then LANGUAGES must contain either 'en-us' or 'en'. If you have not changed either option you do not have to do anything.
The application can be configured by editing the project’s settings.py file.
A tuple of regular expressions matching paths that will not be redirected to add the language prefix. For example, a site with a language selection splash page would add '^/$' as a locale independent path match.
Example:
LOCALE_INDEPENDENT_PATHS = (
r'^/$',
r'^/games/',
r'^/ajax/',
)
LOCALEURL_USE_SESSION (default: False)
Whether to check for a user-selected locale in the Django session as a fallback in case no locale is specified in the URL. If True, the change_locale view will save the chosen locale to the user’s session under the django_language session key. When used and available, the session locale takes precedence over the Accept-Language header fallback (see LOCALEURL_USE_ACCEPT_LANGUAGE above).
A localized URL still takes precedence over a locale in the user’s session. Following a localized link such as one generated using the chlocale filter won’t switch the session’s django_language; only the change_locale view will.
To use this feature, sessions must be in use, and django.contrib.sessions.middleware.SessionMiddleware must come before localeurl.middleware.LocaleURLMiddleware in settings.MIDDLEWARE_CLASSES.