Fix default preferred language should be an admin permitted language

pull/13047/head
SebCorbin 2025-04-19 19:23:30 +02:00 zatwierdzone przez Sage Abdullah
rodzic 47c20b2490
commit c91bff5d21
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: EB1A33CC51CC0217
4 zmienionych plików z 64 dodań i 3 usunięć

Wyświetl plik

@ -40,6 +40,7 @@ Changelog
* Fix: Show the correct privacy status for child collections of private collections (Shlomo Markowitz)
* Fix: Ensure reference index correctly handles models with primary keys not named `id` (Sage Abdullah)
* Fix: On "move page" bulk action, do not prefill the destination with the root page (Stefan Hammer)
* Fix: Ensure the default preferred language respects the `WAGTAILADMIN_PERMITTED_LANGUAGES` setting (Sébastien Corbin)
* Docs: Add missing `django.contrib.admin` to list of apps in "add to Django project" guide (Mohamed Rabiaa)
* Docs: Add tutorial on deploying on Ubuntu to third-party tutorials (Mohammad Fathi Rahman)
* Docs: Document that request_or_site is optional on BaseGenericSetting.load (Matt Westcott)

Wyświetl plik

@ -64,6 +64,7 @@ We have a new pagination interface for all listing views and most choosers, incl
* Show the correct privacy status for child collections of private collections (Shlomo Markowitz)
* Ensure reference index correctly handles models with primary keys not named `id` (Sage Abdullah)
* On "move page" bulk action, do not prefill the destination with the root page (Stefan Hammer)
* Ensure the default preferred language respects the `WAGTAILADMIN_PERMITTED_LANGUAGES` setting (Sébastien Corbin)
### Documentation

Wyświetl plik

@ -2,15 +2,18 @@ import json
from django.contrib.auth.models import AnonymousUser, Permission
from django.template import Context, Template
from django.test import TestCase
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import translation
from django.utils.translation import gettext
from wagtail import hooks
from wagtail.admin.userbar import AccessibilityItem
from wagtail.coreutils import get_dummy_request
from wagtail.models import PAGE_TEMPLATE_VAR, Page, Site
from wagtail.models import PAGE_TEMPLATE_VAR, Locale, Page, Site
from wagtail.test.testapp.models import BusinessChild, BusinessIndex, SimplePage
from wagtail.test.utils import WagtailTestUtils
from wagtail.users.models import UserProfile
from wagtail.utils.deprecation import RemovedInWagtail80Warning
@ -495,6 +498,56 @@ class TestUserbarInPageServe(WagtailTestUtils, TestCase):
self.assertTrue(kwargs.get("called"))
@override_settings(
WAGTAIL_I18N_ENABLED=True,
WAGTAIL_CONTENT_LANGUAGES=[("en", "English"), ("fr", "French")],
WAGTAILADMIN_PERMITTED_LANGUAGES=[("en", "English")],
LANGUAGE_CODE="en",
)
def test_userbar_rendered_in_admin_permitted_language_if_user_has_no_language(self):
french = Locale.objects.create(language_code="fr")
self.homepage.copy_for_translation(french)
french_page = self.page.copy_for_translation(french)
with translation.override("fr"):
response = french_page.serve(self.request)
response.render()
self.assertContains(response, "Go to Wagtail admin")
@override_settings(
WAGTAIL_I18N_ENABLED=True,
WAGTAIL_CONTENT_LANGUAGES=[("en", "English"), ("fr", "French")],
LANGUAGE_CODE="en",
)
def test_userbar_rendered_in_active_language_if_admin_permitted(self):
french = Locale.objects.create(language_code="fr")
self.homepage.copy_for_translation(french)
french_page = self.page.copy_for_translation(french)
with translation.override("fr"):
response = french_page.serve(self.request)
response.render()
with translation.override("fr"):
expected_text = gettext("Go to Wagtail admin")
self.assertContains(response, expected_text)
@override_settings(LANGUAGE_CODE="en")
def test_userbar_rendered_in_user_preferred_language(self):
profile = UserProfile.get_for_user(self.user)
profile.preferred_language = "fr"
profile.save()
response = self.page.serve(self.request)
response.render()
# Ensure we have a French string without tests failing if translation changes
with translation.override("fr"):
expected_text = gettext("Go to Wagtail admin")
self.assertNotEqual(expected_text, "Go to Wagtail admin")
self.assertContains(response, expected_text)
class TestUserbarHooksForChecksPanel(WagtailTestUtils, TestCase):
def setUp(self):

Wyświetl plik

@ -6,6 +6,8 @@ from django.db import models
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
from wagtail.admin.localization import get_available_admin_languages
def upload_avatar_to(instance, filename):
filename, ext = os.path.splitext(filename)
@ -112,7 +114,11 @@ class UserProfile(models.Model):
return cls.objects.get_or_create(user=user)[0]
def get_preferred_language(self):
return self.preferred_language or get_language()
if self.preferred_language:
return self.preferred_language
if (language := get_language()) in dict(get_available_admin_languages()):
return language
return settings.LANGUAGE_CODE
def get_current_time_zone(self):
return self.current_time_zone or settings.TIME_ZONE