I use Django with Mezzanine. I set languages like that:
LANGUAGE_CODE = "en"
MODELTRANSLATION_DEFAULT_LANGUAGE = 'cs'
LANGUAGES = (
('cs', _('Čeština')),
('en', _('Angličtina')),
)
I do NOT use language in URLs.
The problem is, that default language for new session is selected according browser preferences, however it should be EN allways.
How to achieve that?
I can imagine that it is possible to replace https://docs.djangoproject.com/en/2.1/_modules/django/middleware/locale/ with my custom middleware. I guess I should change the function:
def process_request(self, request):
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
language = translation.get_language_from_request(request, check_path=i18n_patterns_used)
language_from_path = translation.get_language_from_path(request.path_info)
if not language_from_path and i18n_patterns_used and not prefixed_default_language:
language = settings.LANGUAGE_CODE
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
Is there a simpler option?
What I should change in the mentioned function to achieve desired behavior?
At the end I figure out that the simplest way is to add my custom middleware:
from django.conf import settings
from django.utils import translation
class ForceLangMiddleware:
def process_request(self, request):
request.META['HTTP_ACCEPT_LANGUAGE'] = "en"
This middleware needs to be loaded before the other middlewares.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.