Django comes with a middleware that determines the current language based on request data. This is the LocaleMiddleware middleware that resides in django.middleware.locale. LocaleMiddleware performs the following tasks:
- If you are using i18_patterns, that is, you use translated URL patterns, it looks for a language prefix in the requested URL to determine the current language.
- If no language prefix is found, it looks for an existing LANGUAGE_SESSION_KEY in the current user's session.
- If the language is not set in the session, it looks for an existing cookie with the current language. A custom name for this cookie can be provided in the LANGUAGE_COOKIE_NAME setting. By default, the name for this cookie is django_language.
- If no cookie is found, it looks for the Accept-Language HTTP header of the request.
- If the Accept-Language header does not specify a language, Django uses the language defined in the LANGUAGE_CODE setting.
By default, Django will use the language defined in the LANGUAGE_CODE setting unless you are using LocaleMiddleware. The process described here only applies when using this middleware.