Activate and deactivate user language in middleware

Fixes #346
This commit is contained in:
Christopher C. Wells 2021-11-28 14:02:23 -05:00
parent 462459964c
commit c28620b3d8
1 changed files with 14 additions and 4 deletions

View File

@ -3,7 +3,7 @@ import time
import pytz import pytz
from django.conf import settings 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 from django.conf.locale.en import formats as formats_en_us
@ -47,10 +47,20 @@ class UserLanguageMiddleware:
def __call__(self, request): def __call__(self, request):
user = request.user user = request.user
if hasattr(user, 'settings') and user.settings.language == 'en-US': if hasattr(user, 'settings'):
# Set the custom user language before generating the response.
translation.activate(user.settings.language)
if user.settings.language == 'en-US':
update_en_us_date_formats() update_en_us_date_formats()
return self.get_response(request) 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: class UserTimezoneMiddleware: