Hello there,

I modified "Advanced Locale from URL middleware" by Jonathan Schemoul a
bit and mixed in some parts of django.middleware.common.

It prepends the language code to the url if no language code was
present in request.path.

Can somebody check the code to suggest possible optimizations and
remove foolishness (especially the regular expressions...)? Untill now,
it just suits the purpose, nothing more.

Thank you,
Gjiro

###

from django.utils.cache import patch_vary_headers
from django.utils import translation
from django.conf import settings
import re

class LocaleURLMiddleware:

        def get_match_from_request (self, request):
                return re.match(r'/(\w\w)/.*', request.path)

        def get_language_from_request (self, request):
                changed = False
                check_langstart = self.get_match_from_request(request)
                lang = settings.LANGUAGE_CODE[:2]
                supported = dict(settings.LANGUAGES)

                if check_langstart is not None: # check if path contains 
language
                        t = check_langstart.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):
                do_prepend = False
                language = self.get_language_from_request(request) # get 
language
                translation.activate(language)
                request.LANGUAGE_CODE = translation.get_language()

                check_langstart = self.get_match_from_request(request)
                supported = dict(settings.LANGUAGES)
                prepend_lang = settings.PREPEND_LANG

                if settings.PREPEND_LANG:

                        check_slashend = re.match(r'/.*/$', request.path) # 
check if path
ends with slash

                        if check_slashend is not None: # path end with slash, 
no image, css,
js.
                                from django import http
                                host = http.get_host(request)
                                old_url = [host, request.path]
                                new_url = old_url[:]

                                if check_langstart is None: # no language 
specified
                                        do_prepend = True
                                        new_url[1] = '/' + language + 
new_url[1] # prepend language

                                else: # language specified
                                        t = check_langstart.group(1)
                                        if t not in supported: # language not 
supported
                                                do_prepend = True
                                                new_url[1] = 
re.sub(r'/([^/]*?)/(.*?)','/'+ language +'/',
request.path) # replace not supported language

                                if prepend_lang and do_prepend: # check 
conditions for redirect

                                        if settings.DEBUG and request.method == 
'POST':
                                                raise RuntimeError, "You called 
this URL via POST, but the URL
doesn't end in a slash and you have APPEND_SLASH set. Django can't
redirect to the slash URL while maintaining POST data. Change your form
to point to %s%s (note the trailing slash), or set APPEND_SLASH=False
in your Django settings." % (new_url[0], new_url[1])
                                        request_path = "/%s/%s" % (language, 
request.path)
                                        if new_url != old_url:
                                                # Redirect
                                                if new_url[0]:
                                                        newurl = "%s://%s%s" % 
(request.is_secure() and 'https' or
'http', new_url[0], new_url[1])
                                                else:
                                                        newurl = new_url[1]
                                                if request.GET:
                                                        newurl += '?' + 
request.GET.urlencode()
                                                return 
http.HttpResponsePermanentRedirect(newurl) # redirect to
new url

                return None

        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

###


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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