| 1 |
from django.conf import settings |
| 2 |
from django.conf.urls.i18n import is_language_prefix_patterns_used |
| 3 |
from django.http import HttpResponseRedirect |
| 4 |
from django.urls import get_script_prefix, is_valid_path |
| 5 |
from django.utils import translation |
| 6 |
from django.utils.cache import patch_vary_headers |
| 7 |
from django.utils.deprecation import MiddlewareMixin |
| 8 |
|
| 9 |
|
| 10 |
class LocaleMiddleware(MiddlewareMixin): |
| 11 |
""" |
| 12 |
Parse a request and decide what translation object to install in the |
| 13 |
current thread context. This allows pages to be dynamically translated to |
| 14 |
the language the user desires (if the language is available). |
| 15 |
""" |
| 16 |
|
| 17 |
response_redirect_class = HttpResponseRedirect |
| 18 |
|
| 19 |
def process_request(self, request): |
| 20 |
urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) |
| 21 |
( |
| 22 |
i18n_patterns_used, |
| 23 |
prefixed_default_language, |
| 24 |
) = is_language_prefix_patterns_used(urlconf) |
| 25 |
language = translation.get_language_from_request( |
| 26 |
request, check_path=i18n_patterns_used |
| 27 |
) |
| 28 |
language_from_path = translation.get_language_from_path(request.path_info) |
| 29 |
if ( |
| 30 |
not language_from_path |
| 31 |
and i18n_patterns_used |
| 32 |
and not prefixed_default_language |
| 33 |
): |
| 34 |
language = settings.LANGUAGE_CODE |
| 35 |
translation.activate(language) |
| 36 |
request.LANGUAGE_CODE = translation.get_language() |
| 37 |
|
| 38 |
def process_response(self, request, response): |
| 39 |
language = translation.get_language() |
| 40 |
language_from_path = translation.get_language_from_path(request.path_info) |
| 41 |
urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) |
| 42 |
( |
| 43 |
i18n_patterns_used, |
| 44 |
prefixed_default_language, |
| 45 |
) = is_language_prefix_patterns_used(urlconf) |
| 46 |
|
| 47 |
if ( |
| 48 |
response.status_code == 404 |
| 49 |
and not language_from_path |
| 50 |
and i18n_patterns_used |
| 51 |
and prefixed_default_language |
| 52 |
): |
| 53 |
# Maybe the language code is missing in the URL? Try adding the |
| 54 |
# language prefix and redirecting to that URL. |
| 55 |
language_path = "/%s%s" % (language, request.path_info) |
| 56 |
path_valid = is_valid_path(language_path, urlconf) |
| 57 |
path_needs_slash = not path_valid and ( |
| 58 |
settings.APPEND_SLASH |
| 59 |
and not language_path.endswith("/") |
| 60 |
and is_valid_path("%s/" % language_path, urlconf) |
| 61 |
) |
| 62 |
|
| 63 |
if path_valid or path_needs_slash: |
| 64 |
script_prefix = get_script_prefix() |
| 65 |
# Insert language after the script prefix and before the |
| 66 |
# rest of the URL |
| 67 |
language_url = request.get_full_path( |
| 68 |
force_append_slash=path_needs_slash |
| 69 |
).replace(script_prefix, "%s%s/" % (script_prefix, language), 1) |
| 70 |
# Redirect to the language-specific URL as detected by |
| 71 |
# get_language_from_request(). HTTP caches may cache this |
| 72 |
# redirect, so add the Vary header. |
| 73 |
redirect = self.response_redirect_class(language_url) |
| 74 |
patch_vary_headers(redirect, ("Accept-Language", "Cookie")) |
| 75 |
return redirect |
| 76 |
|
| 77 |
if not (i18n_patterns_used and language_from_path): |
| 78 |
patch_vary_headers(response, ("Accept-Language",)) |
| 79 |
response.headers.setdefault("Content-Language", language) |
| 80 |
return response |