Repository: wagtail/wagtail

Issue:
Extraneous Locale query with `WAGTAIL_I18N_ENABLED = False`
When i18n is not enabled, we are making a Locale query on every page view

h/t to @Tijani-Dia and his https://github.com/Tijani-Dia/dj-tracker

<details>
<summary>Traceback/reference</summary>
![nn](https://user-images.githubusercontent.com/31622/231769225-ecff22fe-238c-4544-9f30-5d6fe8d3877f.png)
</details>


In pretty much all other places we check that i18n is enabled, but 
https://github.com/wagtail/wagtail/blob/70a6230a650f8ce299d6f883483c37bacfc325d6/wagtail/models/i18n.py#L234-L251 doesn't

Proposal: update `TranslatableMixin.localized_draft` to something like:


```python
    @property
    def localized_draft(self):
        """
        Finds the translation in the current active language.

        If there is no translation in the active language, self is returned.

        Note: This will return translations that are in draft. If you want to exclude
        these, use the ``.localized`` attribute.
        """
        if not getattr(settings, "WAGTAIL_I18N_ENABLED", False):
            return self

        try:
            locale = Locale.get_active()
        except (LookupError, Locale.DoesNotExist):
            return self

        if locale.id == self.locale_id:
            return self

        return self.get_translation_or_none(locale) or self
```


Discussion:
@zerolab I can confirm this issue exists, when disabling `WAGTAIL_I18N_ENABLED` this code locale query still runs when viewing any page.

I think your solution makes sense and agree we do that check in other places that run a similar query.

Resolving patch:
diff --git a/wagtail/models/i18n.py b/wagtail/models/i18n.py
--- a/wagtail/models/i18n.py
+++ b/wagtail/models/i18n.py
@@ -240,6 +240,9 @@ def localized_draft(self):
         Note: This will return translations that are in draft. If you want to exclude
         these, use the ``.localized`` attribute.
         """
+        if not getattr(settings, "WAGTAIL_I18N_ENABLED", False):
+            return self
+
         try:
             locale = Locale.get_active()
         except (LookupError, Locale.DoesNotExist):


Test patch:
diff --git a/wagtail/tests/test_page_model.py b/wagtail/tests/test_page_model.py
--- a/wagtail/tests/test_page_model.py
+++ b/wagtail/tests/test_page_model.py
@@ -3496,6 +3496,7 @@ def test_always_defaults_to_parent_locale(self):
         self.assertEqual(page.locale, fr_locale)
 
 
+@override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestLocalized(TestCase):
     fixtures = ["test.json"]
 
@@ -3520,6 +3521,13 @@ def test_localized_different_language(self):
                 self.event_page.localized_draft, self.fr_event_page.page_ptr
             )
 
+    @override_settings(WAGTAIL_I18N_ENABLED=False)
+    def test_localized_different_language_with_wagtail_i18n_enabled_false(self):
+        """Should return the same page if WAGTAIL_I18N_ENABLED is False"""
+        with translation.override("fr"):
+            self.assertEqual(self.event_page.localized, self.event_page)
+            self.assertEqual(self.event_page.localized_draft, self.event_page)
+
     def test_localized_different_language_unpublished(self):
         # We shouldn't autolocalize if the translation is unpublished
         self.fr_event_page.unpublish()
diff --git a/wagtail/tests/test_translatablemixin.py b/wagtail/tests/test_translatablemixin.py
--- a/wagtail/tests/test_translatablemixin.py
+++ b/wagtail/tests/test_translatablemixin.py
@@ -2,7 +2,7 @@
 
 from django.conf import settings
 from django.core import checks
-from django.test import TestCase
+from django.test import TestCase, override_settings
 
 from wagtail.models import Locale
 from wagtail.test.i18n.models import (
@@ -21,6 +21,7 @@ def make_test_instance(model=None, **kwargs):
     return model.objects.create(**kwargs)
 
 
+@override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestTranslatableMixin(TestCase):
     def setUp(self):
         language_codes = dict(settings.LANGUAGES).keys()
@@ -158,6 +159,7 @@ def test_copy_clusterable_model_for_translation(self):
         self.assertEqual(copy_translatable_child.locale, self.another_locale)
 
 
+@override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestLocalized(TestCase):
     def setUp(self):
         self.en_locale = Locale.objects.get()