1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #11585 -- Added ability to translate and prefix URL patterns with a language code as an alternative method for language discovery. Many thanks to Orne Brocaar for his initial work and Carl Meyer for feedback.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16405 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel
2011-06-15 17:29:10 +00:00
parent 62bb4b8c37
commit 896e3c69c7
29 changed files with 750 additions and 57 deletions

View File

@@ -1,5 +1,7 @@
"this is the locale selecting middleware that will look at accept headers"
"This is the locale selecting middleware that will look at accept headers"
from django.core.urlresolvers import get_resolver, LocaleRegexURLResolver
from django.http import HttpResponseRedirect
from django.utils.cache import patch_vary_headers
from django.utils import translation
@@ -18,8 +20,26 @@ class LocaleMiddleware(object):
request.LANGUAGE_CODE = translation.get_language()
def process_response(self, request, response):
language = translation.get_language()
translation.deactivate()
if (response.status_code == 404 and
not translation.get_language_from_path(request.path_info)
and self.is_language_prefix_patterns_used()):
return HttpResponseRedirect(
'/%s%s' % (language, request.get_full_path()))
patch_vary_headers(response, ('Accept-Language',))
if 'Content-Language' not in response:
response['Content-Language'] = translation.get_language()
translation.deactivate()
response['Content-Language'] = language
return response
def is_language_prefix_patterns_used(self):
"""
Returns `True` if the `LocaleRegexURLResolver` is used
at root level of the urlpatterns, else it returns `False`.
"""
for url_pattern in get_resolver(None).url_patterns:
if isinstance(url_pattern, LocaleRegexURLResolver):
return True
return False