From 26de524247ca510f9e7b50f57e435c8179e137e6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 6 Oct 2021 13:01:29 -0700 Subject: [PATCH] Update language on login and edit preference --- bookwyrm/settings.py | 1 + bookwyrm/views/helpers.py | 11 ++++++++++- bookwyrm/views/login.py | 5 +++-- bookwyrm/views/preferences/edit_user.py | 5 +++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index b2f90487..00ce9e4a 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -30,6 +30,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) LOCALE_PATHS = [ os.path.join(BASE_DIR, "locale"), ] +LANGUAGE_COOKIE_NAME = env.str("LANGUAGE_COOKIE_NAME", "django_language") DEFAULT_AUTO_FIELD = "django.db.models.AutoField" diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index f31b41ff..f28d0102 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -7,8 +7,9 @@ from dateutil.parser import ParserError from requests import HTTPError from django.http import Http404 +from django.utils import translation -from bookwyrm import activitypub, models +from bookwyrm import activitypub, models, settings from bookwyrm.connectors import ConnectorException, get_data from bookwyrm.status import create_generated_note from bookwyrm.utils import regex @@ -144,3 +145,11 @@ def load_date_in_user_tz_as_utc(date_str: str, user: models.User) -> datetime: return date.replace(tzinfo=user_tz).astimezone(dateutil.tz.UTC) except ParserError: return None + + +def set_language(user, response): + """Updates a user's language""" + if user.preferred_language: + translation.activate(user.preferred_language) + response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user.preferred_language) + return response diff --git a/bookwyrm/views/login.py b/bookwyrm/views/login.py index e96d421a..91fda35a 100644 --- a/bookwyrm/views/login.py +++ b/bookwyrm/views/login.py @@ -11,6 +11,7 @@ from django.views.decorators.debug import sensitive_variables, sensitive_post_pa from bookwyrm import forms, models from bookwyrm.settings import DOMAIN +from bookwyrm.views.helpers import set_language # pylint: disable=no-self-use @@ -55,8 +56,8 @@ class Login(View): login(request, user) user.update_active_date() if request.POST.get("first_login"): - return redirect("get-started-profile") - return redirect(request.GET.get("next", "/")) + return set_language(user, redirect("get-started-profile")) + return set_language(user, redirect(request.GET.get("next", "/"))) # maybe the user is pending email confirmation if models.User.objects.filter( diff --git a/bookwyrm/views/preferences/edit_user.py b/bookwyrm/views/preferences/edit_user.py index 275304d7..a1b5b363 100644 --- a/bookwyrm/views/preferences/edit_user.py +++ b/bookwyrm/views/preferences/edit_user.py @@ -11,6 +11,7 @@ from django.utils.decorators import method_decorator from django.views import View from bookwyrm import forms +from bookwyrm.views.helpers import set_language # pylint: disable=no-self-use @@ -33,9 +34,9 @@ class EditUser(View): data = {"form": form, "user": request.user} return TemplateResponse(request, "preferences/edit_user.html", data) - save_user_form(form) + user = save_user_form(form) - return redirect("user-feed", request.user.localname) + return set_language(user, redirect("user-feed", request.user.localname)) def save_user_form(form):