Last active
October 11, 2017 13:50
-
-
Save phpdude/9388407 to your computer and use it in GitHub Desktop.
Django LocaleByDomainMiddleware - Locale By Domain Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOCALE_DOMAINS = ( | |
('www.mysite.com', 'en'), | |
('mysite.com', 'en'), | |
('ru.mysite.com', 'ru'), | |
('de.mysite.com', 'de'), | |
('fr.mysite.com', 'fr'), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?