import logging from django.utils import translation from django.conf import settings class DomainBasedLanguageMiddleware(object): """ Set the language for the site based on the subdomain the request is being served on. For example, serving on 'fr.domain.com' would make the language French (fr). """ def process_request(self, request): host = request.get_host() if host in settings.LANGUAGE_DOMAINS: lang = settings.LANGUAGE_DOMAINS.get(host) logging.debug("Choosing language: {0}".format(lang)) translation.deactivate() translation.activate(lang) request.LANGUAGE_CODE = lang