mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
translation: Always set language cookie when switching language
Django 3.0 will now always set the language cookie. It will stop setting the session language in Django 4.0. To avoid breaking current behavior, always set the language cookie when switching language. "To limit creation of sessions and hence favor some caching strategies, django.views.i18n.set_language() will stop setting the user’s language in the session in Django 4.0. Since Django 2.1, the language is always stored in the LANGUAGE_COOKIE_NAME cookie." Tests: - All relevant functional tests run. - Repeat login and user page editing tests. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
57931353d3
commit
379e0af9c9
@ -25,8 +25,9 @@ def get_language_from_request(request):
|
||||
def set_language(request, response, language_code):
|
||||
"""Set the language in session or as a separate cookie.
|
||||
|
||||
Sending language code as None removes the preference. If response is None,
|
||||
cookies are not touched and setting/deleting language cookie will not work.
|
||||
Sending language code as None removes the preference. response is not
|
||||
optional as Django 3.0 up always set the language cookie and Django 4.0
|
||||
will no longer set the language in the session.
|
||||
|
||||
"""
|
||||
if not language_code:
|
||||
@ -47,12 +48,11 @@ def set_language(request, response, language_code):
|
||||
translation.activate(language_code)
|
||||
if hasattr(request, 'session'):
|
||||
request.session[translation.LANGUAGE_SESSION_KEY] = language_code
|
||||
else:
|
||||
response.set_cookie(
|
||||
settings.LANGUAGE_COOKIE_NAME,
|
||||
language_code,
|
||||
max_age=settings.LANGUAGE_COOKIE_AGE,
|
||||
path=settings.LANGUAGE_COOKIE_PATH,
|
||||
domain=settings.LANGUAGE_COOKIE_DOMAIN,
|
||||
)
|
||||
|
||||
response.set_cookie(
|
||||
settings.LANGUAGE_COOKIE_NAME,
|
||||
language_code,
|
||||
max_age=settings.LANGUAGE_COOKIE_AGE,
|
||||
path=settings.LANGUAGE_COOKIE_PATH,
|
||||
domain=settings.LANGUAGE_COOKIE_DOMAIN,
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user