From 379e0af9c991ad113a0d6d197276eaf698869406 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 23 Sep 2021 18:39:35 -0700 Subject: [PATCH] translation: Always set language cookie when switching language MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: James Valleroy --- plinth/translation.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plinth/translation.py b/plinth/translation.py index 74a881bbd..af941af44 100644 --- a/plinth/translation.py +++ b/plinth/translation.py @@ -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, + )