There was a task to switch the interface language in Django, depending on the domain. On stackoverflow suggest using transylvania module that is too complex for such a simple task. To solve this problem, I decided to create a simple middleware, which would detect HTTP_HOST header and change the language depending on what domain was specified Django settings.
First of all let's create a simple dict in django settings.py:
LANGUAGES_DOMAINS = { 'somedomain.com': 'en', }
Where somedomain.com is a domain name, and 'en' the language code that you want to apply for this domain.
Let's write a simple middleware:
from django.conf import settings from django.utils import translation class DomainLocaleMiddleware(object): """ Set language regarding of domain """ def process_request(self, request): if request.META.has_key('HTTP_ACCEPT_LANGUAGE'): # Totally ignore the browser settings... del request.META['HTTP_ACCEPT_LANGUAGE'] current_domain = request.META['HTTP_HOST'] lang_code = settings.LANGUAGES_DOMAINS.get(current_domain) if lang_code: translation.activate(lang_code) request.LANGUAGE_CODE = lang_code
If the domain has not been specified in LANGUAGES_DOMAINS it will use the language by default.
2016-05-06
