fromdjango.confimportsettingsfromdjango.utils.cacheimportpatch_vary_headersfromdjango.utilsimporttranslationclassSessionBasedLocaleMiddleware(object):""" This Middleware saves the desired content language in the user session. The SessionMiddleware has to be activated. """defprocess_request(self,request):ifrequest.method=='GET'and'lang'inrequest.GET:language=request.GET['lang']request.session['language']=languageelif'language'inrequest.session:language=request.session['language']else:language=translation.get_language_from_request(request)forlanginsettings.LANGUAGES:iflang[0]==language:translation.activate(language)request.LANGUAGE_CODE=translation.get_language()defprocess_response(self,request,response):patch_vary_headers(response,('Accept-Language',))if'Content-Language'notinresponse:response['Content-Language']=translation.get_language()translation.deactivate()returnresponse