Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django LocaleByDomainMiddleware - Locale By Domain Middleware
from django.conf import settings
from django.utils import translation
class LocaleByDomainMiddleware():
def process_request(self, request):
host = request.META['HTTP_HOST'].lower()
locales = dict(settings.LOCALE_DOMAINS)
if not host in locales:
language_code = settings.LANGUAGE_CODE
else:
language_code = locales[host]
request.LANGUAGE_CODE = language_code
translation.activate(language_code)
def process_response(self, request, response):
response['Content-Language'] = translation.get_language()
translation.deactivate()
return response
LOCALE_DOMAINS = (
('www.mysite.com', 'en'),
('mysite.com', 'en'),
('ru.mysite.com', 'ru'),
('de.mysite.com', 'de'),
('fr.mysite.com', 'fr'),
)
@andriy-kravets

This comment has been minimized.

Copy link

@andriy-kravets andriy-kravets commented Nov 27, 2014

Hello!
Your code is very useful, but I have situation when I want change locale by buttons and regarding domain.
And I haven't ideas how to do this.
Maybe you can help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment