2011-06-16 01:29:10 +08:00
|
|
|
"This is the locale selecting middleware that will look at accept headers"
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-08-03 13:41:15 +08:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2012-03-02 19:07:36 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.urlresolvers import (is_valid_path, get_resolver,
|
|
|
|
LocaleRegexURLResolver)
|
2011-06-16 01:29:10 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2005-11-04 12:59:46 +08:00
|
|
|
from django.utils.cache import patch_vary_headers
|
|
|
|
from django.utils import translation
|
|
|
|
|
2012-03-02 19:07:36 +08:00
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class LocaleMiddleware(object):
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
This is a very simple middleware that parses a request
|
|
|
|
and decides 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).
|
|
|
|
"""
|
2012-11-17 20:14:12 +08:00
|
|
|
response_redirect_class = HttpResponseRedirect
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-02-24 02:41:33 +08:00
|
|
|
def __init__(self):
|
2013-08-03 13:41:15 +08:00
|
|
|
self._supported_languages = OrderedDict(settings.LANGUAGES)
|
2013-02-24 02:41:33 +08:00
|
|
|
self._is_language_prefix_patterns_used = False
|
|
|
|
for url_pattern in get_resolver(None).url_patterns:
|
|
|
|
if isinstance(url_pattern, LocaleRegexURLResolver):
|
|
|
|
self._is_language_prefix_patterns_used = True
|
|
|
|
break
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def process_request(self, request):
|
2012-02-18 21:37:30 +08:00
|
|
|
check_path = self.is_language_prefix_patterns_used()
|
|
|
|
language = translation.get_language_from_request(
|
|
|
|
request, check_path=check_path)
|
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-02-24 02:41:33 +08:00
|
|
|
language_from_path = translation.get_language_from_path(
|
2013-10-20 07:33:10 +08:00
|
|
|
request.path_info, supported=self._supported_languages
|
2013-02-24 02:41:33 +08:00
|
|
|
)
|
|
|
|
if (response.status_code == 404 and not language_from_path
|
|
|
|
and self.is_language_prefix_patterns_used()):
|
2012-03-02 19:07:36 +08:00
|
|
|
urlconf = getattr(request, 'urlconf', None)
|
|
|
|
language_path = '/%s%s' % (language, request.path_info)
|
2013-01-12 04:27:51 +08:00
|
|
|
path_valid = is_valid_path(language_path, urlconf)
|
|
|
|
if (not path_valid and settings.APPEND_SLASH
|
|
|
|
and not language_path.endswith('/')):
|
|
|
|
path_valid = is_valid_path("%s/" % language_path, urlconf)
|
2012-03-03 06:35:26 +08:00
|
|
|
|
2013-01-12 04:27:51 +08:00
|
|
|
if path_valid:
|
2012-03-03 06:35:26 +08:00
|
|
|
language_url = "%s://%s/%s%s" % (
|
2013-10-09 02:30:29 +08:00
|
|
|
request.scheme, request.get_host(), language,
|
|
|
|
request.get_full_path())
|
2012-11-17 20:14:12 +08:00
|
|
|
return self.response_redirect_class(language_url)
|
2013-02-28 20:45:21 +08:00
|
|
|
|
2013-02-24 02:41:33 +08:00
|
|
|
if not (self.is_language_prefix_patterns_used()
|
|
|
|
and language_from_path):
|
|
|
|
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
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
def is_language_prefix_patterns_used(self):
|
|
|
|
"""
|
|
|
|
Returns `True` if the `LocaleRegexURLResolver` is used
|
|
|
|
at root level of the urlpatterns, else it returns `False`.
|
|
|
|
"""
|
2013-02-24 02:41:33 +08:00
|
|
|
return self._is_language_prefix_patterns_used
|