app/django/middleware/locale.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 14 Dec 2008 00:33:29 +0000
changeset 739 b245c2b814c5
parent 323 ff1a9aa48cfd
permissions -rw-r--r--
Added support for example texts Initial patch by Benjamin, some minor fixes by Sverre. Patch by: Benjamin Lyaunzbe, Sverre Rabbelier

"this is the locale selecting middleware that will look at accept headers"

from django.utils.cache import patch_vary_headers
from django.utils import translation

class LocaleMiddleware(object):
    """
    This is a very simple middleware that parses a request
    and decides what translation object to install in the current
    thread context. This allows pages to be dynamically
    translated to the language the user desires (if the language
    is available, of course).
    """

    def process_request(self, request):
        language = translation.get_language_from_request(request)
        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        patch_vary_headers(response, ('Accept-Language',))
        if 'Content-Language' not in response:
            response['Content-Language'] = translation.get_language()
        translation.deactivate()
        return response