A middleware that permits to change the locale based on the url  was
posted here 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...

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 vrsion of the url you
are on, but without the language inside, to make "change language"
buttons.

Have fun !
----
Jon

Here goes 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_code in supported and lang_code is not 
None:
                                        return lang

                                lang = request.COOKIES.get('django_language', 
None)
                                if lang_code in supported and lang_code is not 
None:
                                        return lang
                return lang

        def process_request(self, request):
                language = self.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',))
                translation.deactivate()
                return response

#######
from django.conf import settings
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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to