How to explicitly set django_language
in Django
session?
Thanks a lot...
And if you will use a version >= Django 1.8
. Here it is how we could use that:
from django.utils.translation import LANGUAGE_SESSION_KEY
def someview (request):
...
request.session[LANGUAGE_SESSION_KEY] = 'en'
If you want your users to be able to specify language, make sure that LocaleMiddleware
is enabled:
MIDDLEWARE_CLASSES = (
...
'django.middleware.locale.LocaleMiddleware',
...
)
Then Django will look for the user's language preference in that order (see get_language_from_request
in trans_real.py):
request.path_info
, if i18n_patterns are used;request.session[settings.LANGUAGE_SESSION_KEY]
;request.COOKIES[settings.LANGUAGE_COOKIE_NAME]
;request.META['HTTP_ACCEPT_LANGUAGE']
, until accepted one is found;settings.LANGUAGE_CODE
.So the most straightforward way to set language explicitly in Django session is to rewrite request.session[settings.LANGUAGE_SESSION_KEY]
:
def someview (request):
...
request.session[settings.LANGUAGE_SESSION_KEY] = 'en'
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With