From 07dc1f5a704bd18b5961aefe49ae653e7f4e85cc Mon Sep 17 00:00:00 2001 From: Agrendalath Date: Wed, 23 Nov 2022 16:40:51 +0100 Subject: [PATCH] fix: apply language specified in Site Configuration Previously, this solution was implemented in the `dark_lang` middleware. However, the `lang_pref` middleware started setting the user-preferred language by default, which resulted in overriding the language cookie twice for a single request (making this feature unusable). In this commit, the language from Site Configuration will be applied after the one specified in user preferences, to allow setting a language globally for a specific Site. --- openedx/core/djangoapps/dark_lang/middleware.py | 11 ----------- openedx/core/djangoapps/lang_pref/middleware.py | 12 ++++++++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/openedx/core/djangoapps/dark_lang/middleware.py b/openedx/core/djangoapps/dark_lang/middleware.py index 36c4798a7981..325c7b1e7c8c 100644 --- a/openedx/core/djangoapps/dark_lang/middleware.py +++ b/openedx/core/djangoapps/dark_lang/middleware.py @@ -17,7 +17,6 @@ from openedx.core.djangoapps.dark_lang import DARK_LANGUAGE_KEY from openedx.core.djangoapps.dark_lang.models import DarkLangConfig from openedx.core.djangoapps.lang_pref.helpers import set_language_cookie -from openedx.core.djangoapps.site_configuration.helpers import get_value from openedx.core.djangoapps.user_api.preferences.api import get_user_preference # If django 1.7 or higher is used, the right-side can be updated with new-style codes. @@ -98,20 +97,10 @@ def process_response(self, request, response): Apply user's dark lang preference as a cookie for future requests. """ if DarkLangConfig.current().enabled: - self._set_site_or_microsite_language(request, response) self._activate_preview_language(request, response) return response - def _set_site_or_microsite_language(self, request, response): - """ - Apply language specified in site configuration. - """ - language = get_value('LANGUAGE_CODE', None) - if language: - request.session[LANGUAGE_SESSION_KEY] = language - set_language_cookie(request, response, language) - def _fuzzy_match(self, lang_code): """Returns a fuzzy match for lang_code""" match = None diff --git a/openedx/core/djangoapps/lang_pref/middleware.py b/openedx/core/djangoapps/lang_pref/middleware.py index d0a787c5daa5..205888fa2048 100644 --- a/openedx/core/djangoapps/lang_pref/middleware.py +++ b/openedx/core/djangoapps/lang_pref/middleware.py @@ -11,6 +11,7 @@ from openedx.core.djangoapps.dark_lang.models import DarkLangConfig from openedx.core.djangoapps.lang_pref import LANGUAGE_HEADER, LANGUAGE_KEY from openedx.core.djangoapps.lang_pref import helpers as lang_pref_helpers +from openedx.core.djangoapps.site_configuration.helpers import get_value from openedx.core.djangoapps.user_api.errors import UserAPIInternalError, UserAPIRequestError from openedx.core.djangoapps.user_api.preferences.api import get_user_preference, set_user_preference from openedx.core.lib.mobile_utils import is_request_from_mobile_app @@ -54,6 +55,15 @@ def process_request(self, request): if LANGUAGE_SESSION_KEY in request.session and request.session[LANGUAGE_SESSION_KEY] != cookie_lang: del request.session[LANGUAGE_SESSION_KEY] + @staticmethod + def _set_site_language(request, response): + """ + Apply language specified in site configuration. + """ + if language := get_value('LANGUAGE_CODE'): + request.session[LANGUAGE_SESSION_KEY] = language + lang_pref_helpers.set_language_cookie(request, response, language) + def process_response(self, request, response): # lint-amnesty, pylint: disable=missing-function-docstring # If the user is logged in, check for their language preference. Also check for real user # if current user is a masquerading user, @@ -86,4 +96,6 @@ def process_response(self, request, response): # lint-amnesty, pylint: disable= else: lang_pref_helpers.unset_language_cookie(response) + self._set_site_language(request, response) + return response