From c28620b3d8f66b37f25e13e312b37293126f3afa Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 28 Nov 2021 14:02:23 -0500 Subject: [PATCH] Activate and deactivate user language in middleware Fixes #346 --- babybuddy/middleware.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/babybuddy/middleware.py b/babybuddy/middleware.py index a00b72c7..07c1472a 100644 --- a/babybuddy/middleware.py +++ b/babybuddy/middleware.py @@ -3,7 +3,7 @@ import time import pytz from django.conf import settings -from django.utils import timezone +from django.utils import timezone, translation from django.conf.locale.en import formats as formats_en_us @@ -47,10 +47,20 @@ class UserLanguageMiddleware: def __call__(self, request): user = request.user - if hasattr(user, 'settings') and user.settings.language == 'en-US': - update_en_us_date_formats() + if hasattr(user, 'settings'): + # Set the custom user language before generating the response. + translation.activate(user.settings.language) - return self.get_response(request) + if user.settings.language == 'en-US': + update_en_us_date_formats() + + response = self.get_response(request) + + # Deactivate the translation before the response is sent so it not + # reused in other threads. + translation.deactivate() + + return response class UserTimezoneMiddleware: