From 5af72ce100b6c1c49a4899091facced676905b2b Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sun, 13 Oct 2024 15:23:34 -0300 Subject: [PATCH] test --- app/apps/common/middleware/localization.py | 30 ++++++++++++---------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/app/apps/common/middleware/localization.py b/app/apps/common/middleware/localization.py index 1dd5040..a4fb76f 100644 --- a/app/apps/common/middleware/localization.py +++ b/app/apps/common/middleware/localization.py @@ -18,26 +18,28 @@ class LocalizationMiddleware: user_language = "auto" user_timezone = "auto" + # Set timezone if tz and user_timezone == "auto": - timezone.activate(zoneinfo.ZoneInfo(tz)) + timezone_to_activate = zoneinfo.ZoneInfo(tz) elif user_timezone != "auto": - timezone.activate(zoneinfo.ZoneInfo(user_timezone)) + timezone_to_activate = zoneinfo.ZoneInfo(user_timezone) else: - timezone.activate(zoneinfo.ZoneInfo("UTC")) + timezone_to_activate = zoneinfo.ZoneInfo("UTC") + # Set language if user_language and user_language != "auto": - language = user_language + language_to_activate = user_language else: - language = translation.get_language_from_request(request) + language_to_activate = translation.get_language_from_request(request) - translation.activate(language) - request.LANGUAGE_CODE = translation.get_language() + # Apply timezone and language to the request + request.timezone = timezone_to_activate + request.language = language_to_activate - response = self.get_response(request) + # Wrap the response in a custom function to handle activation + def wrapped_response(request): + with timezone.override(request.timezone): + with translation.override(request.language): + return self.get_response(request) - patch_vary_headers(response, ("Accept-Language",)) - response.headers.setdefault("Content-Language", translation.get_language()) - - translation.deactivate() - - return response + return wrapped_response(request)