PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/django/middleware/locale.py

https://code.google.com/p/mango-py/
Python | 25 lines | 14 code | 4 blank | 7 comment | 3 complexity | 614a9d3b847641b8dbba802414eda07e MD5 | raw file
 1"this is the locale selecting middleware that will look at accept headers"
 2
 3from django.utils.cache import patch_vary_headers
 4from django.utils import translation
 5
 6class LocaleMiddleware(object):
 7    """
 8    This is a very simple middleware that parses a request
 9    and decides what translation object to install in the current
10    thread context. This allows pages to be dynamically
11    translated to the language the user desires (if the language
12    is available, of course).
13    """
14
15    def process_request(self, request):
16        language = translation.get_language_from_request(request)
17        translation.activate(language)
18        request.LANGUAGE_CODE = translation.get_language()
19
20    def process_response(self, request, response):
21        patch_vary_headers(response, ('Accept-Language',))
22        if 'Content-Language' not in response:
23            response['Content-Language'] = translation.get_language()
24        translation.deactivate()
25        return response