Hi all. In work I need midleware that can do 2 things: 1 First check is there a language in requested url e.g. www.example.com/fr/ and if is there check is the language is in supported. If supported sets that language to the traslantion, if not make redirect to the default site - www.example.com/uk/ 2 If there isn't language in url, middleware check for it in http header, and again when found language which is supported make redirect to url like this: www.example.com/domain_for_that_language/ so e.g. for svedish it will be se and if middleware doesn't find a language or language is not supported make redirect to the default language. I must use 2 additional constant in my settings.py first is LANGUAGE_DOMAIN = 'uk' - this is default domain and DOMAINS tuple which contain mapping from national language to national domain. DOMAINS = ( ('en','uk'), ('en','us'), ('sv','se'), ) and here is the code: ################################################################################## from django.utils.cache import patch_vary_headers from django.utils import translation from django.http import HttpResponseRedirect, get_host, HttpResponse from settings import LANGUAGE_CODE, LANGUAGES, LANGUAGE_DOMAIN, DOMAINS from mysite.urls import przedrostek #only for test, cause I use http://localhost:8000/test/ not http://localhost:8000/ from re import match, compile
class LocaleURLMiddleware: def __init__(self): self.check_slashend_pattern = compile(r'/.*/$') self.check_admin_pattern = compile(r'/admin/.*') self.language = compile(r"/"+przedrostek+"(\w\w)/.*") def process_request(self, request): check_slashend = match(self.check_slashend_pattern, request.path) if check_slashend is None:# js, css,img we exit from middleware return None check_admin = match(self.check_admin_pattern, request.path) if check_admin is not None:# for admin we exit from middleware return None language = match(self.language, request.path) if language is not None and language.group(1): languageAccepted = False lang = None for i in DOMAINS: if language.group(1) == i[1]:#we compare domain not language languageAccepted = True lang = i[0] if languageAccepted and lang is not None: translation.activate(lang) request.LANGUAGE_CODE = translation.get_language() request.session['Locale_domain'] = language.group(1) else:#redirect to default language host = get_host(request) return HttpResponseRedirect('http://'+host+'/'+przedrostek+LANGUAGE_DOMAIN+'/') else:#there is no language in url lang = request.META['HTTP_ACCEPT_LANGUAGE'].split(',')[0].split(';')[0] if lang:#we check if language from header is supported languageAccepted = False domainToRedirect = None for i in DOMAINS: if lang == i[0]:#we compare language not domain languageAccepted = True domainToRedirect = i[1] if languageAccepted and domainToRedirect is not None:#we redirect to language from header translation.activate(lang) request.LANGUAGE_CODE = translation.get_language() host = get_host(request) return HttpResponseRedirect('http://'+host+'/'+przedrostek+domainToRedirect+'/') else:#we redirect to default language host = get_host(request) return HttpResponseRedirect('http://'+host+'/'+przedrostek+LANGUAGE_DOMAIN+'/') else:#we redirect to default language host = get_host(request) return HttpResponseRedirect('http://'+host+'/'+przedrostek+LANGUAGE_DOMAIN+'/') def process_response(self, request, response): patch_vary_headers(response, ('Accept-Language',)) translation.deactivate() ############################################################################ The code works fine but is so slow, on my developer machine I must wait about half minute to see my start page. Can anyone please help me and show me where I made mistake?? This is my first middleware Thank for any help Gregor --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---