diff --git a/wagtail/admin/tests/pages/test_create_page.py b/wagtail/admin/tests/pages/test_create_page.py index 5b2734b945..ccc1041774 100644 --- a/wagtail/admin/tests/pages/test_create_page.py +++ b/wagtail/admin/tests/pages/test_create_page.py @@ -1743,6 +1743,7 @@ class TestLocaleSelectorOnRootPage(WagtailTestUtils, TestCase): self.assertContains(response, 'id="status-sidebar-english"') + # Should show a link to switch to another locale add_translation_url = ( reverse( "wagtailadmin_pages:add", @@ -1752,6 +1753,50 @@ class TestLocaleSelectorOnRootPage(WagtailTestUtils, TestCase): ) self.assertContains(response, f'href="{add_translation_url}"') + # Should not show a link to switch to the current locale + self_translation_url = ( + reverse( + "wagtailadmin_pages:add", + args=["demosite", "homepage", self.root_page.id], + ) + + "?locale=en" + ) + self.assertNotContains(response, f'href="{self_translation_url}"') + + def test_locale_selector_selected(self): + response = self.client.get( + reverse( + "wagtailadmin_pages:add", + args=["demosite", "homepage", self.root_page.id], + ) + + "?locale=fr" + ) + + self.assertContains(response, 'id="status-sidebar-french"') + + # Should render the locale input with the currently selected locale + self.assertContains(response, '') + + # Should show a link to switch to another locale + add_translation_url = ( + reverse( + "wagtailadmin_pages:add", + args=["demosite", "homepage", self.root_page.id], + ) + + "?locale=en" + ) + self.assertContains(response, f'href="{add_translation_url}"') + + # Should not show a link to switch to the current locale + self_translation_url = ( + reverse( + "wagtailadmin_pages:add", + args=["demosite", "homepage", self.root_page.id], + ) + + "?locale=fr" + ) + self.assertNotContains(response, f'href="{self_translation_url}"') + @override_settings(WAGTAIL_I18N_ENABLED=False) def test_locale_selector_not_present_when_i18n_disabled(self): response = self.client.get( diff --git a/wagtail/admin/views/pages/create.py b/wagtail/admin/views/pages/create.py index 849b552b44..2ac6644d38 100644 --- a/wagtail/admin/views/pages/create.py +++ b/wagtail/admin/views/pages/create.py @@ -390,6 +390,7 @@ class CreateView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View): "form": self.form, "next": self.next_url, "has_unsaved_changes": self.has_unsaved_changes, + "locale": self.locale, "media": media, } ) @@ -413,7 +414,8 @@ class CreateView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View): + "?" + urlencode({"locale": locale.language_code}), } - for locale in Locale.objects.all() + # Do not show the switcher for the current locale + for locale in Locale.objects.exclude(pk=self.locale.pk) ] else: diff --git a/wagtail/admin/views/pages/edit.py b/wagtail/admin/views/pages/edit.py index 8303940efa..f4f2b7f521 100644 --- a/wagtail/admin/views/pages/edit.py +++ b/wagtail/admin/views/pages/edit.py @@ -939,6 +939,7 @@ class EditView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View): and user_perms.can_lock(), "user_can_unlock": isinstance(self.lock, BasicLock) and user_perms.can_unlock(), + "locale": self.locale, "media": media, } )