mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-28 08:03:36 +00:00
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>
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""
|
|
Utility methods for managing translations.
|
|
"""
|
|
|
|
from django.conf import settings
|
|
from django.utils import translation
|
|
|
|
|
|
def get_language_from_request(request):
|
|
"""Get the language in the session or as separate cookie.
|
|
|
|
Django methods should be used for regular cases. This is only useful for
|
|
very narrow cases.
|
|
|
|
"""
|
|
if hasattr(request, 'session'):
|
|
language_code = request.session.get(translation.LANGUAGE_SESSION_KEY)
|
|
if language_code:
|
|
return language_code
|
|
|
|
return request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
|
|
|
|
|
|
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. 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:
|
|
if hasattr(request, 'session'):
|
|
try:
|
|
del request.session[translation.LANGUAGE_SESSION_KEY]
|
|
except KeyError:
|
|
pass
|
|
|
|
if response:
|
|
try:
|
|
response.delete_cookie(settings.LANGUAGE_COOKIE_NAME)
|
|
except KeyError:
|
|
pass
|
|
|
|
return
|
|
|
|
translation.activate(language_code)
|
|
if hasattr(request, 'session'):
|
|
request.session[translation.LANGUAGE_SESSION_KEY] = language_code
|
|
|
|
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,
|
|
)
|