Ignore HTTP Accept-Language headers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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 process_request(self, request):
        if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
            del request.META['HTTP_ACCEPT_LANGUAGE']

More like this

  1. Multilingual site based on domain - not accept header and django_session by hermansc 2 years, 3 months ago
  2. Per-site vary cache on language by fneumann 6 years, 6 months ago
  3. Getting dynamic model choices in newforms by ubernostrum 7 years, 1 month ago
  4. Improved Accept header middleware by ludvig.ericson 5 years, 7 months ago
  5. Accept Header Middleware by kioopi 6 years ago

Comments

ubernostrum (on May 7, 2007):

You could just disable the LocaleMiddleware... :)

#

jefferson (on May 8, 2007):

hi fonso,

i see the same problem as you do.

i´ve changed the locale middleware to this to have my default language set:

def process_request(self, request):
    if request.session.get('django_language'):
        language = translation.get_language_from_request(request)
    elif request.COOKIES.get('django_language'):
        language = translation.get_language_from_request(request)
    else:
        language = 'de'
    #language = translation.get_language_from_request(request)
    translation.activate(language)
    request.LANGUAGE_CODE = translation.get_language()


# not changed
def process_response(self, request, response):
    ...

#

fonso (on May 9, 2007):

hi jefferson,

Yes, AFAIK, if your settings.LANGUAGE_CODE is 'de', the posted middleware would have exactly the same effect your changes do.

#

dariusdamalakas (on February 1, 2011):

Disabling LocaleMiddleware does not help. The goal here is to ignore HTTP Accept-Language headers, and choose the default language which is defined in settings.py After that user should be able to still change his language, if he wants to

This middle ware does exactly what we need. Thanks alot!

#

(Forgotten your password?)