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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
You could just disable the
LocaleMiddleware
... :)#
hi fonso,
i see the same problem as you do.
i´ve changed the locale middleware to this to have my default language set:
#
hi jefferson,
Yes, AFAIK, if your settings.LANGUAGE_CODE is 'de', the posted middleware would have exactly the same effect your changes do.
#
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!
#
thanks ubernostrum!, your solution worked for me :)
#
Please login first before commenting.