2012-03-02 19:07:36 +08:00
|
|
|
from django.conf import settings
|
2016-02-29 16:24:19 +08:00
|
|
|
from django.conf.urls.i18n import is_language_prefix_patterns_used
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2016-02-29 16:24:19 +08:00
|
|
|
from django.urls import get_script_prefix, is_valid_path
|
|
|
|
from django.utils import translation
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.cache import patch_vary_headers
|
2016-06-14 15:41:58 +08:00
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2012-03-02 19:07:36 +08:00
|
|
|
|
2016-06-14 15:41:58 +08:00
|
|
|
class LocaleMiddleware(MiddlewareMixin):
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Parse a request and decide what translation object to install in the
|
|
|
|
current thread context. This allows pages to be dynamically translated to
|
|
|
|
the language the user desires (if the language is available, of course).
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2012-11-17 20:14:12 +08:00
|
|
|
response_redirect_class = HttpResponseRedirect
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
def process_request(self, request):
|
2015-12-15 01:36:09 +08:00
|
|
|
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
|
2016-02-29 16:24:19 +08:00
|
|
|
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
|
|
|
|
language = translation.get_language_from_request(request, check_path=i18n_patterns_used)
|
2016-03-10 22:08:53 +08:00
|
|
|
language_from_path = translation.get_language_from_path(request.path_info)
|
|
|
|
if not language_from_path and i18n_patterns_used and not prefixed_default_language:
|
2016-02-29 16:24:19 +08:00
|
|
|
language = settings.LANGUAGE_CODE
|
2005-11-04 12:59:46 +08:00
|
|
|
translation.activate(language)
|
|
|
|
request.LANGUAGE_CODE = translation.get_language()
|
|
|
|
|
|
|
|
def process_response(self, request, response):
|
2011-06-16 01:29:10 +08:00
|
|
|
language = translation.get_language()
|
2013-11-12 14:54:01 +08:00
|
|
|
language_from_path = translation.get_language_from_path(request.path_info)
|
2015-12-15 01:36:09 +08:00
|
|
|
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
|
2016-02-29 16:24:19 +08:00
|
|
|
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
|
|
|
|
|
2016-11-05 19:56:34 +08:00
|
|
|
if (response.status_code == 404 and not language_from_path and
|
|
|
|
i18n_patterns_used and prefixed_default_language):
|
|
|
|
# Maybe the language code is missing in the URL? Try adding the
|
|
|
|
# language prefix and redirecting to that URL.
|
2012-03-02 19:07:36 +08:00
|
|
|
language_path = '/%s%s' % (language, request.path_info)
|
2013-01-12 04:27:51 +08:00
|
|
|
path_valid = is_valid_path(language_path, urlconf)
|
2015-03-23 03:04:31 +08:00
|
|
|
path_needs_slash = (
|
|
|
|
not path_valid and (
|
2016-04-04 08:37:32 +08:00
|
|
|
settings.APPEND_SLASH and not language_path.endswith('/') and
|
|
|
|
is_valid_path('%s/' % language_path, urlconf)
|
2015-03-23 03:04:31 +08:00
|
|
|
)
|
|
|
|
)
|
2012-03-03 06:35:26 +08:00
|
|
|
|
2015-03-23 03:04:31 +08:00
|
|
|
if path_valid or path_needs_slash:
|
2014-08-14 21:32:50 +08:00
|
|
|
script_prefix = get_script_prefix()
|
2015-03-14 06:40:14 +08:00
|
|
|
# Insert language after the script prefix and before the
|
|
|
|
# rest of the URL
|
2015-03-23 03:04:31 +08:00
|
|
|
language_url = request.get_full_path(force_append_slash=path_needs_slash).replace(
|
2015-03-14 06:40:14 +08:00
|
|
|
script_prefix,
|
|
|
|
'%s%s/' % (script_prefix, language),
|
|
|
|
1
|
2014-08-14 21:32:50 +08:00
|
|
|
)
|
2012-11-17 20:14:12 +08:00
|
|
|
return self.response_redirect_class(language_url)
|
2013-02-28 20:45:21 +08:00
|
|
|
|
2016-02-29 16:24:19 +08:00
|
|
|
if not (i18n_patterns_used and language_from_path):
|
2013-02-24 02:41:33 +08:00
|
|
|
patch_vary_headers(response, ('Accept-Language',))
|
2008-08-09 23:04:45 +08:00
|
|
|
if 'Content-Language' not in response:
|
2011-06-16 01:29:10 +08:00
|
|
|
response['Content-Language'] = language
|
2005-11-04 12:59:46 +08:00
|
|
|
return response
|