Skip to content

Instantly share code, notes, and snippets.

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
language_code = locales[host]
request.LANGUAGE_CODE = language_code
def process_response(self, request, response):
response['Content-Language'] = translation.get_language()
return response
('', 'en'),
('', 'en'),
('', 'ru'),
('', 'de'),
('', 'fr'),

This comment has been minimized.

Copy link

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

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