Skip to content

Instantly share code, notes, and snippets.

@mikicz
Forked from vstoykov/force_default_language_middleware.py
Last active September 26, 2016 13:23
Show Gist options
  • Save mikicz/19cadfb9abc9476b00814902e252777d to your computer and use it in GitHub Desktop.
Save mikicz/19cadfb9abc9476b00814902e252777d to your computer and use it in GitHub Desktop.
Force django to use settings.LANGUAGE_CODE for default language and not use equest.META['HTTP_ACCEPT_LANGUAGE'] for Django >= 1.10
class ForceDefaultLanguageMiddleware(object):
"""
Ignore Accept-Language HTTP headers
This will force the I18N machinery to always choose settings.LANGUAGE_CODE
as the default initial language, unless another one is set via sessions or cookies
Should be installed *before* any middleware that checks request.META['HTTP_ACCEPT_LANGUAGE'],
namely django.middleware.locale.LocaleMiddleware
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
self.process_request(request)
response = self.get_response(request)
return response
def process_request(self, request):
if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
del request.META['HTTP_ACCEPT_LANGUAGE']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment