mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-07-08 11:40:25 +00:00
translation: Don't use session for storing lang pref in Django 4.0
Helps: #2228. - In Django 3.0, storing language preference in session key is deprecated and only the language cookie is used. In Django 4.0, this functionality is completely removed along with the constant LANGUAGE_SESSION_KEY. - Debian stable (Bullseye) uses Django 2.2 and this depends on code to store language preference in session key. To work on Django 2.2 through 4.0, check if the constant is available and then set the session key of the constant is found. Tests: - Change language in user edit page and see that it is persisted. - After logout, the new language is still set. - Changing language as anonymous user works. - Run tests in stable, testing and unstable containers. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
16ab0e4fc9
commit
5d0a7c6d16
@ -7,6 +7,11 @@ from django.conf import settings
|
||||
from django.utils import translation
|
||||
|
||||
|
||||
def _should_use_sessions(request):
|
||||
return hasattr(request, 'session') and hasattr(translation,
|
||||
'LANGUAGE_SESSION_KEY')
|
||||
|
||||
|
||||
def get_language_from_request(request):
|
||||
"""Get the language in the session or as separate cookie.
|
||||
|
||||
@ -14,7 +19,7 @@ def get_language_from_request(request):
|
||||
very narrow cases.
|
||||
|
||||
"""
|
||||
if hasattr(request, 'session'):
|
||||
if _should_use_sessions(request):
|
||||
language_code = request.session.get(translation.LANGUAGE_SESSION_KEY)
|
||||
if language_code:
|
||||
return language_code
|
||||
@ -31,7 +36,8 @@ def set_language(request, response, language_code):
|
||||
|
||||
"""
|
||||
if not language_code:
|
||||
if hasattr(request, 'session'):
|
||||
|
||||
if _should_use_sessions(request):
|
||||
try:
|
||||
del request.session[translation.LANGUAGE_SESSION_KEY]
|
||||
except KeyError:
|
||||
@ -46,7 +52,7 @@ def set_language(request, response, language_code):
|
||||
return
|
||||
|
||||
translation.activate(language_code)
|
||||
if hasattr(request, 'session'):
|
||||
if _should_use_sessions(request):
|
||||
request.session[translation.LANGUAGE_SESSION_KEY] = language_code
|
||||
|
||||
response.set_cookie(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user