Hi,

I have a problem with Django - I'm trying to build a website which
will work on a couple of different domains and each domain should have
a different language/locale.

Example:
domain.com -> English
domain2.pl -> Polish

The SITE_ID should change accordingly when I visit the corresponding
domain (I have 2 records in django_site table)

So - my solution to this is to use a snippet for SITE_ID:
http://www.djangosnippets.org/snippets/1099/
In settings.py it reads now:
from threading import local
SITE_THREAD_INFO = local()
SITE_THREAD_INFO.SITE_ID = 1
class SiteIDHook(object):
    def __int__(self):
        return SITE_THREAD_INFO.SITE_ID
    def __hash__(self):
        return SITE_THREAD_INFO.SITE_ID
SITE_ID = SiteIDHook()


And a middleware which handles the SITE_ID changes + my modifications
based on LocaleMiddleware, which should change locale (LANGUAGE_CODE).

middleware.py:
from django.conf import settings
from django.contrib.sites.models import Site
from django.http import HttpResponseRedirect

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

import logging
log = logging.getLogger('multisite')

class MultiSiteMiddleware(object):
    def process_request(self, request):
        settings.SITE_THREAD_INFO.SITE_ID = 1

        host = request.META.get('HTTP_HOST').split(':')[0]
        if host.startswith('www.'): host = host[4:]

        log.debug('host=' + host)

        if host:
            try:
                site = Site.objects.get(domain=host)
                settings.SITE_THREAD_INFO.SITE_ID = site.id
                if '.pl' in host:
                    translation.activate('pl-pl')
                    request.LANGUAGE_CODE = translation.get_language()
                else:
                    translation.activate('en-us')
                    request.LANGUAGE_CODE = translation.get_language()

            except Site.DoesNotExist:
                settings.SITE_THREAD_INFO.SITE_ID = 1

    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


middleware.py is attached just after SessionMiddleware as suggested in
the docs.

The problem is that Django has some problems with it - it sometimes
shows some strings translated and some not. When I browse just one
site/domain everything is OK. But when I open another tab and browse
the other domain (in different language) then this weirdness happens.

What's wrong with that code, and how to do it properly?

Regards,
MS

--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to