Created
April 15, 2011 21:48
-
-
Save issackelly/922533 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
import django.core.exceptions | |
from django.http import HttpResponsePermanentRedirect | |
from django.utils import translation | |
# TODO importing undocumented function | |
from django.utils.translation.trans_real import parse_accept_lang_header | |
from localeurl import settings as localeurl_settings | |
from localeurl import utils | |
# Make sure the default language is in the list of supported languages | |
assert utils.supported_language(settings.LANGUAGE_CODE) is not None, \ | |
"Please ensure that settings.LANGUAGE_CODE is in settings.LANGUAGES." | |
class LocaleURLMiddleware(object): | |
""" | |
Middleware that sets the language based on the request path prefix and | |
strips that prefix from the path. It will also automatically redirect any | |
path without a prefix, unless PREFIX_DEFAULT_LOCALE is set to True. | |
Exceptions are paths beginning with MEDIA_URL (if | |
settings.LOCALE_INDEPENDENT_MEDIA_URL is set) or matching any regular | |
expression from LOCALE_INDEPENDENT_PATHS from the project settings. | |
For example, the path '/en/admin/' will set request.LANGUAGE_CODE to 'en' | |
and request.path to '/admin/'. | |
Alternatively, the language is set by the first component of the domain | |
name. For example, a request on 'fr.example.com' would set the language to | |
French. | |
If you use this middleware the django.core.urlresolvers.reverse function | |
is be patched to return paths with locale prefix (see models.py). | |
""" | |
def __init__(self): | |
if not settings.USE_I18N: | |
raise django.core.exceptions.MiddlewareNotUsed() | |
def process_request(self, request): | |
locale, path = utils.strip_path(request.path_info) | |
try: | |
request_language = request.LANGUAGE_CODE | |
if request_language != locale: | |
locale = request_language | |
except AttributeError: | |
pass | |
if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: | |
accept_langs = filter(lambda x: x, [utils.supported_language(lang[0]) | |
for lang in | |
parse_accept_lang_header( | |
request.META.get('HTTP_ACCEPT_LANGUAGE', ''))]) | |
if accept_langs: | |
locale = accept_langs[0] | |
locale_url = utils.locale_url(path, locale) | |
if locale_url != request.path_info: | |
if request.META.get("QUERY_STRING", ""): | |
locale_url = "%s?%s" % (locale_url, | |
request.META['QUERY_STRING']) | |
return HttpResponsePermanentRedirect(locale_url) | |
request.path_info = path | |
if not locale: | |
try: | |
locale = request.LANGUAGE_CODE | |
except AttributeError: | |
locale = settings.LANGUAGE_CODE | |
translation.activate(locale) | |
request.LANGUAGE_CODE = translation.get_language() | |
def process_response(self, request, response): | |
if 'Content-Language' not in response: | |
response['Content-Language'] = translation.get_language() | |
translation.deactivate() | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment