Repository: wagtail/wagtail

Issue:
Generic choosers: hide/remove locale filter if i18n is not enabled (4.0rc1)
First: the new generic choosers (#8480) seem to work very good so far in our tests, we can finally replace the various (often outdated) model chooser implementations with this flexible core feature, amazing work! 👍

### Issue Summary

We often use model choosers for specific page models, so they can be searched/selected easily, without using the page tree (used by the PageChooser).
Migrating our code to a `ChooserViewSet` for this page model for projects without wagtail-i18n (no translations) now shows a language dropdown in the chooser modal with only the single (untranslated) default language:
 
![wagtail_generic_chooser_single_locale](https://user-images.githubusercontent.com/468164/185344631-5a49f3d8-e82c-41ca-9abc-bfc7d063fdce.gif)

This could also happen for other (thirdparty) models, which use the `TranslatableMixin`, when used in a project with only a single language (no wagtail i18n enabled).

The chooser works, so it is not really a blocker, but I hope a fix will make it into the final 4.0 :pray: 

**Possible solution:**
Maybe check for `WAGTAIL_I18N_ENABLED` when the `LocaleFilterMixin` gets added to the filter form here:
https://github.com/wagtail/wagtail/blob/1fcf4cacbb3c5f34daa10e4e375cb5d75a2c8aae/wagtail/admin/views/generic/chooser.py#L112-L113

There is other code that already uses this option, e.g.:
https://github.com/wagtail/wagtail/blob/1fcf4cacbb3c5f34daa10e4e375cb5d75a2c8aae/wagtail/admin/views/chooser.py#L206

### Steps to Reproduce

1. Start a new project with `wagtail start myproject`
2. Create a `home/viewsets.py`:
```python
from wagtail.admin.viewsets.chooser import ChooserViewSet

class HomePageChooserViewSet(ChooserViewSet):
    model = "home.HomePage"

home_page_chooser_viewset = HomePageChooserViewSet("home_page_chooser")
```
3. Create `home/blocks.py`:
```python
from .viewsets import home_page_chooser_viewset

HomePageChooserBlock = home_page_chooser_viewset.get_block_class(
    name="HomePageChooserBlock", module_path="home.blocks"
)
```
4. Create `home/wagtail_hooks.py`:
```python
from wagtail import hooks
from .viewsets import home_page_chooser_viewset

@hooks.register("register_admin_viewset")
def register_viewset():
    return home_page_chooser_viewset
```
5. Edit `home/models.py` as follows:
```python
from wagtail.admin.panels import FieldPanel
from wagtail.fields import StreamField
from wagtail.models import Page
from .blocks import HomePageChooserBlock

class HomePage(Page):
    content_panels = Page.content_panels + [FieldPanel("content")]
    content = StreamField(
        [
            ("home", HomePageChooserBlock()),
        ],
        blank=True,
        use_json_field=True,
    )
```
6. `./manage.py makemigrations && ./manage.py migrate && ./manage.py createsuperuser && ./manage.py runserver`
7. In the CMS, edit the home page and add a home block using the chooser.

-   I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

### Technical details

-   Python version: 3.10
-   Django version: 4.1
-   Wagtail version: 4.0rc1 (nightly https://releases.wagtail.org/nightly/dist/wagtail-4.0.dev20220817-py3-none-any.whl)
-   Browser version: Firefox 103



Discussion:

Resolving patch:
diff --git a/wagtail/admin/views/generic/chooser.py b/wagtail/admin/views/generic/chooser.py
--- a/wagtail/admin/views/generic/chooser.py
+++ b/wagtail/admin/views/generic/chooser.py
@@ -1,3 +1,4 @@
+from django.conf import settings
 from django.contrib.admin.utils import unquote
 from django.core.exceptions import (
     ImproperlyConfigured,
@@ -109,7 +110,9 @@ def get_filter_form_class(self):
                     bases.insert(0, SearchFilterMixin)
                 if issubclass(self.model_class, CollectionMember):
                     bases.insert(0, CollectionFilterMixin)
-                if issubclass(self.model_class, TranslatableMixin):
+
+                i18n_enabled = getattr(settings, "WAGTAIL_I18N_ENABLED", False)
+                if i18n_enabled and issubclass(self.model_class, TranslatableMixin):
                     bases.insert(0, LocaleFilterMixin)
 
             return type(


Test patch:
diff --git a/wagtail/snippets/tests/test_snippets.py b/wagtail/snippets/tests/test_snippets.py
--- a/wagtail/snippets/tests/test_snippets.py
+++ b/wagtail/snippets/tests/test_snippets.py
@@ -2721,6 +2721,21 @@ def test_not_searchable(self):
         # filter_form should not have a search field
         self.assertFalse(self.get().context["filter_form"].fields.get("q"))
 
+    @override_settings(WAGTAIL_I18N_ENABLED=False)
+    def test_locale_filter_requires_i18n_enabled(self):
+        self.url_args = ["snippetstests", "translatablesnippet"]
+        fr_locale = Locale.objects.create(language_code="fr")
+
+        TranslatableSnippet.objects.create(text="English snippet")
+        TranslatableSnippet.objects.create(text="French snippet", locale=fr_locale)
+
+        response = self.get()
+
+        # Check the filter is omitted
+        response_html = response.json()["html"]
+        self.assertNotIn("data-chooser-modal-search-filter", response_html)
+        self.assertNotIn('name="locale"', response_html)
+
     @override_settings(WAGTAIL_I18N_ENABLED=True)
     def test_filter_by_locale(self):
         self.url_args = ["snippetstests", "translatablesnippet"]