You may want to set the active language for the current session explicitly. Perhaps a user's language preference is retrieved from another system, for example. You've already been introduced to django.utils.translation.activate()
. That applies to the current thread only. To persist the language for the entire session, also modify LANGUAGE_SESSION_KEY
in the session:
from django.utils import translation user_language = 'fr' translation.activate(user_language) request.session[translation.LANGUAGE_SESSION_KEY] = user_language
You would typically want to use both: django.utils.translation.activate()
will change the language for this thread, and modifying the session makes this preference persist in future requests.
If you are not using sessions, the language will persist in a cookie, whose name is configured in LANGUAGE_COOKIE_NAME
. For example:
from django.utils import translation from django import http from django.conf...