This section describes how to install the localeurl application in your Django project.
Setup consists of installing the middleware and adding
to the installed applications list.
settings.MIDDLEWARE_CLASSES. It must come before
settings.APPEND_SLASHwill not work. Make sure Django’s built-in
LocaleMiddlewareis not in your
LocaleURLMiddlewarereplaces it and the two will not work together. It must also come after
django.contrib.sessions.middleware.SessionMiddlewareif you plan using the session fallback (see
settings.INSTALLED_APPS. Because the application needs to replace the standard
urlresolvers.reversefunction, 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')), ... )
settings.LANGUAGE_CODEor its root language is in
settings.LANGUAGES. For example, if
LANGUAGE_CODE == 'en-us'then
LANGUAGESmust contain either
'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
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.
LOCALE_INDEPENDENT_PATHS = ( r'^/$', r'^/games/', r'^/ajax/', )
- Whether paths starting with
settings.MEDIA_URL(if it is a path, i.e. not a full URL) are considered to be locale-independent.
- Whether paths starting with
settings.STATIC_URL(if it is a path, i.e. not a full URL) are considered to be locale-independent.
- Whether to add the prefix for the default language
settings.LANGUAGE_CODE). For example, if
LANGUAGE_CODE == 'en'then the path
/about/will be passed to the URL resolver unchanged and
/en/about/will be redirected to
- Whether to check the
Accept-Languageheader from the browser as an intermediate fallback in case no locale is specified in the URL. (The default behavior, preserved for backwards compatibility, is to fallback directly to
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
change_localeview will save the chosen locale to the user’s session under the
django_languagesession key. When used and available, the session locale takes precedence over the
Accept-Languageheader fallback (see
A localized URL still takes precedence over a locale in the user’s session. Following a localized link such as one generated using the
chlocalefilter won’t switch the session’s
django_language; only the
To use this feature, sessions must be in use, and
django.contrib.sessions.middleware.SessionMiddlewaremust come before
- Whether to use a permanent redirect (301 status code) or temporary
redirect (302) when redirecting users from the no-locale version of a URL
to the default locale (or the locale specified in their Accept-Language
LOCALEURL_USE_ACCEPT_LANGUAGEis True). If Accept-Language is not used, these redirects will never change (as long as the default locale never changes), so 301 (the default) is a fine choice. If you use Accept-Language you may want to consider switching this to
False, as the redirect will then be dependent on the user’s Accept-Language header.