The Articles

Language from Django URL

July 2, 2006 at 9:44 p.m. Tagged as: Blog, Frameworks, Programming, Django


A middleware that permits to change the locale based on the url was posted on the django-users group one month ago by Atlithorn.

For example, when you go to /fr/article/*** you get the article in french, and when you go to /en/article/*** you get the article in english…

Langues / Languages
This article is bilingual, your are seeing the english version.
Cet article est bilingue, le voir en Français.
Go directly to the code.

I’ve gone a little further making a version that sets a cookie when you go to a localized url, and then reuses it when you go to an url that doesn’t have any language set…

Why is it usefull ? For login, logout, comments, etc… views and any 3rd party module that doesn’t take into account our language url stuff ! :)

I’m also providing a function that generates a version of the url you are on, but without the language inside, to make “change language” buttons.

The code
_from django.utils.cache import patch_vary_headers_
from django.utils import translation
class LocaleURLMiddleware:
    def get_language_from_request (self,request):
        from django.conf import settings
        import re
        supported = dict(settings.LANGUAGES)
        lang = settings.LANGUAGE_CODE[:2]
        check = re.match(r”/(\w\w)/.*”, request.path)
        changed = False
        if check is not None:
            t = check.group(1)
            if t in supported:
                lang = t
                if hasattr(request, 'session’):
                    request.session['django_language’] = lang
                else:
                    response.set_cookie('django_language’, lang)
                changed = True
        if not changed:
            if hasattr(request, 'session’):
                lang = request.session.get('django_language’, None)
                if lang in supported and lang is not None:
                    return lang
            else:
                lang = request.COOKIES.get('django_language’, None)
                if lang in supported and lang is not None:
                    return lang
        return lang
    def process_request(self, request):
        from django.conf import settings
        language = self.get_language_from_request(request)
        if language is None:
            language = settings.LANGUAGE_CODE[:2]
        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()
    def process_response(self, request, response):
        patch_vary_headers(response, ('Accept-Language’,))
        translation.deactivate()
        return response

def get_absolute_path_without_lang(request):
    for lang in settings.LANGUAGES:
        if ‘’ + str(lang[0]) + ‘’ in request.path:
            return request.path.replace(’/’ + str(lang[0]) + ‘’, ‘’)
    return request.path


Comments

blog comments powered by Disqus

Last Projects

Foresight Linux Website Project type: Web

My sites

Paris Envies Everything you need to know to go out in Paris ! SmoothGallery A javascript slideshow and gallery based on mootools !


Switch to English Passez à la version Française Flux RSS des Articles XHtml 1.1 Valide