Skip to content

Instantly share code, notes, and snippets.

@javierhonduco
Forked from alexissmirnov/locale.py
Last active August 29, 2015 14:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Django QueryParameterLocaleMiddleware
from django.middleware import locale
from django.utils import translation
class QueryParameterLocaleMiddleware(locale.LocaleMiddleware):
"""
Django's LocaleMiddleware picks the locale setting of the request from
multiple sources such as a cookie, Accept-Language header and server-side
session.
This middleware adds one more way to specify the language by supplying
'locale' query parameter. It's value should be set to the same language
codes as required for Accept-Language.
Works with any HTTP verb (GET, POST, whatever)
"""
def process_request(self, request):
"""
Overrides the parent class to try getting the language code from
request parameter.
"""
if request.REQUEST.has_key('locale'):
translation.activate(request.REQUEST['locale'])
request.LANGUAGE_CODE = translation.get_language()
else:
super(QueryParameterLocaleMiddleware, self).process_request(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment