Login

Ignore HTTP Accept-Language headers

Author:
fonso
Posted:
May 7, 2007
Language:
Python
Version:
.96
Tags:
middleware i18n l10n locale
Score:
0 (after 0 ratings)

A little tiny middleware that, when used in multilingual sites, will make Django I18N ignore any Accept-Language headers in the request, thus ensuring that every first-time visitor (with no explicit language preference set via session or cookie) will see the site in the language specified by settings.LANGUAGE_CODE.

(Please note that I think that overriding user preferences is generally a bad practice, but I had my reasons to use it :) )

 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 4 years, 7 months ago
  2. Per-site vary cache on language by fneumann 8 years, 10 months ago
  3. Getting dynamic model choices in newforms by ubernostrum 9 years, 6 months ago
  4. LocaleMiddleware without browser language discovery by ivellios 1 year, 6 months ago
  5. Improved Accept header middleware by ludvig.ericson 7 years, 11 months ago

Comments

ubernostrum (on May 7, 2007):
<p>You could just disable the LocaleMiddleware... :)</p>

#

jefferson (on May 8, 2007):
<p>hi fonso,</p> <p>i see the same problem as you do.</p> <p>i´ve changed the locale middleware to this to have my default language set:</p> <pre>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): ... </pre>

#

fonso (on May 9, 2007):
<p>hi jefferson,</p> <p>Yes, AFAIK, if your settings.LANGUAGE_CODE is 'de', the posted middleware would have exactly the same effect your changes do.</p>

#

dariusdamalakas (on February 1, 2011):
<p>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</p> <p>This middle ware does exactly what we need. Thanks alot!</p>

#

charlesoliveros (on September 18, 2015):
<p>thanks ubernostrum!, your solution worked for me :)</p>

#

Please login first before commenting.