From c580342a9528b26d1bd044ad50740f1020d4fc01 Mon Sep 17 00:00:00 2001
From: Mark Niehues <mark.niehues@posteo.de>
Date: Mon, 26 Feb 2024 15:21:32 +0100
Subject: [PATCH] Fix date formatting in form builder email content

Ensure that the date_format util correctly gets passed a format reference, not the resolved format.
Fixes #11696
---
 CHANGELOG.txt                              |  1 +
 CONTRIBUTORS.md                            |  1 +
 docs/releases/6.1.md                       |  1 +
 wagtail/contrib/forms/models.py            |  5 ++---
 wagtail/contrib/forms/tests/test_models.py | 26 ++++++++++++++++++++++
 5 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index c737d1e70d..a73c120ee4 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -21,6 +21,7 @@ Changelog
  * Fix: Consistently remove model's `verbose_name` in group edit view when listing custom permissions (Sage Abdullah, Neeraj Yetheendran, Omkar Jadhav)
  * Fix: Resolve issue local development of docs when running `make livehtml` (Sage Abdullah)
  * Fix: Resolve issue with unwanted padding in chooser modal listings (Sage Abdullah)
+ * Fix: Ensure form builder emails that have date or datetime fields correctly localize dates based on the configured `LANGUAGE_CODE` (Mark Niehues)
  * Docs: Add contributing development documentation on how to work with a fork of Wagtail (Nix Asteri, Dan Braghis)
  * Docs: Make sure the settings panel is listed in tabbed interface examples (Tibor Leupold)
  * Docs: Update content and page names to their US spelling instead of UK spelling (Victoria Poromon)
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 90caf96671..16d1bb9d72 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -805,6 +805,7 @@
 * Viktor Szépe
 * Pranith Beeram
 * Maranda Provance
+* Mark Niehues
 
 ## Translators
 
diff --git a/docs/releases/6.1.md b/docs/releases/6.1.md
index 35f6ab1fb9..3ae5f7c7d2 100644
--- a/docs/releases/6.1.md
+++ b/docs/releases/6.1.md
@@ -34,6 +34,7 @@ depth: 1
  * Consistently remove model's `verbose_name` in group edit view when listing custom permissions (Sage Abdullah, Neeraj Yetheendran, Omkar Jadhav)
  * Resolve issue local development of docs when running `make livehtml` (Sage Abdullah)
  * Resolve issue with unwanted padding in chooser modal listings (Sage Abdullah)
+ * Ensure form builder emails that have date or datetime fields correctly localize dates based on the configured `LANGUAGE_CODE` (Mark Niehues)
 
 
 ### Documentation
diff --git a/wagtail/contrib/forms/models.py b/wagtail/contrib/forms/models.py
index 809d905708..897dc13e2f 100644
--- a/wagtail/contrib/forms/models.py
+++ b/wagtail/contrib/forms/models.py
@@ -1,7 +1,6 @@
 import datetime
 import os
 
-from django.conf import settings
 from django.core.serializers.json import DjangoJSONEncoder
 from django.core.validators import validate_email
 from django.db import models
@@ -365,9 +364,9 @@ class EmailFormMixin(models.Model):
 
             # Format dates and datetime(s) with SHORT_DATE(TIME)_FORMAT
             if isinstance(value, datetime.datetime):
-                value = date_format(value, settings.SHORT_DATETIME_FORMAT)
+                value = date_format(value, "SHORT_DATETIME_FORMAT")
             elif isinstance(value, datetime.date):
-                value = date_format(value, settings.SHORT_DATE_FORMAT)
+                value = date_format(value, "SHORT_DATE_FORMAT")
 
             content.append(f"{field.label}: {value}")
 
diff --git a/wagtail/contrib/forms/tests/test_models.py b/wagtail/contrib/forms/tests/test_models.py
index 51d8ac9512..4da4da7126 100644
--- a/wagtail/contrib/forms/tests/test_models.py
+++ b/wagtail/contrib/forms/tests/test_models.py
@@ -770,6 +770,32 @@ class TestCleanedDataEmails(TestCase):
         self.assertEqual(len(mail.outbox), 3)
         self.assertIn("Datetime: 12/21/1910 9:19 p.m.", mail.outbox[2].body)
 
+    @override_settings(USE_I18N=True, LANGUAGE_CODE="de")
+    def test_date_localization(self):
+        self.client.post(
+            "/contact-us/",
+            {
+                "date": "2017-12-31",
+            },
+        )
+
+        # Check the email
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertIn("Date: 31.12.2017", mail.outbox[0].body)
+
+    @override_settings(USE_I18N=True, LANGUAGE_CODE="de")
+    def test_datetime_localization(self):
+        self.client.post(
+            "/contact-us/",
+            {
+                "datetime": "1910-12-21 21:19:12",
+            },
+        )
+
+        # Check the email
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertIn("Datetime: 21.12.1910 21:19", mail.outbox[0].body)
+
 
 class TestIssue798(WagtailTestUtils, TestCase):
     fixtures = ["test.json"]