Django QueryParameterLocaleMiddleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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