From cd131ddec8e0b98b4178ea1eeb062a7d03e90ab6 Mon Sep 17 00:00:00 2001
From: zerolab <dan@zerolab.org>
Date: Wed, 19 Apr 2023 16:04:51 +0100
Subject: [PATCH] Skip Locale query when WAGTAIL_I18N_ENABLED is False

- Fixes #10329
---
 CHANGELOG.txt                           | 3 ++-
 docs/releases/5.0.md                    | 1 +
 wagtail/models/i18n.py                  | 3 +++
 wagtail/tests/test_page_model.py        | 8 ++++++++
 wagtail/tests/test_translatablemixin.py | 4 +++-
 5 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 13aaf1b14e..ea3c592b53 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -85,7 +85,8 @@ Changelog
  * Fix: Fix incorrect API serialisation for document `download_url` when `WAGTAILDOCS_SERVE_METHOD` is `direct` (Swojak-A)
  * Fix: Fix template configuration of snippets index results view (fidoriel, Sage Abdullah)
  * Fix: Prevent long preview mode names from making the select element overflow the side panel (Sage Abdullah)
- * Autosize text area field will now correctly resize when switching between comments toggle states (Suyash Srivastava)
+ * Fix: Autosize text area field will now correctly resize when switching between comments toggle states (Suyash Srivastava)
+ * Fix: When i18n is not enabled, avoid making a Locale query on every page view (Dan Braghis)
  * Docs: Add code block to make it easier to understand contribution docs (Suyash Singh)
  * Docs: Add new "Icons" page for icons customisation and reuse across the admin interface (Coen van der Kamp, Thibaud Colas)
  * Docs: Fix broken formatting for MultiFieldPanel / FieldRowPanel permission kwarg docs (Matt Westcott)
diff --git a/docs/releases/5.0.md b/docs/releases/5.0.md
index bc03255bce..1a81a6ac03 100644
--- a/docs/releases/5.0.md
+++ b/docs/releases/5.0.md
@@ -141,6 +141,7 @@ We hope this new theme will bring accessibility improvements for users who perfe
  * Fix incorrect API serialisation for document `download_url` when `WAGTAILDOCS_SERVE_METHOD` is `direct` (Swojak-A)
  * Fix template configuration of snippets index results view (fidoriel, Sage Abdullah)
  * Prevent long preview mode names from making the select element overflow the side panel (Sage Abdullah)
+ * When i18n is not enabled, avoid making a Locale query on every page view (Dan Braghis)
 
 ### Documentation
 
diff --git a/wagtail/models/i18n.py b/wagtail/models/i18n.py
index 9d7e89e2ef..b90033cbef 100644
--- a/wagtail/models/i18n.py
+++ b/wagtail/models/i18n.py
@@ -240,6 +240,9 @@ class TranslatableMixin(models.Model):
         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):
diff --git a/wagtail/tests/test_page_model.py b/wagtail/tests/test_page_model.py
index 7dc650d500..4ebd6a0995 100644
--- a/wagtail/tests/test_page_model.py
+++ b/wagtail/tests/test_page_model.py
@@ -3496,6 +3496,7 @@ class TestDefaultLocale(TestCase):
         self.assertEqual(page.locale, fr_locale)
 
 
+@override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestLocalized(TestCase):
     fixtures = ["test.json"]
 
@@ -3520,6 +3521,13 @@ class TestLocalized(TestCase):
                 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
index 9a2ccf7464..009538d497 100644
--- a/wagtail/tests/test_translatablemixin.py
+++ b/wagtail/tests/test_translatablemixin.py
@@ -2,7 +2,7 @@ from unittest.mock import patch
 
 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 @@ class TestTranslatableMixin(TestCase):
         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()