PageRenderTime 22ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/django/middleware/locale.py

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