Skip to content

Instantly share code, notes, and snippets.

@phpdude
Last active October 11, 2017 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phpdude/9388407 to your computer and use it in GitHub Desktop.
Save phpdude/9388407 to your computer and use it in GitHub Desktop.
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
Copy link

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