From b266e54ba917280657b3961c5fe6ff46752d5ea8 Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Sun, 28 Apr 2024 09:58:38 +0200 Subject: [PATCH] Bug: Enable template tag `richtext` to convert lazy text strings (#11901) Fixes #11900 --- CHANGELOG.txt | 1 + docs/releases/6.2.md | 1 + wagtail/templatetags/wagtailcore_tags.py | 3 +++ wagtail/tests/tests.py | 5 +++++ 4 files changed, 10 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 020c50e49bf..b7f98ba8602 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -8,6 +8,7 @@ Changelog * Support a `HOSTNAMES` parameter on `WAGTAILFRONTENDCACHE` to define which hostnames a backend should respond to (Jake Howard, sponsored by Oxfam America) * Refactor redirects edit view to use the generic `EditView` and breadcrumbs (Rohit Sharma) * Fix: Make `WAGTAILIMAGES_CHOOSER_PAGE_SIZE` setting functional again (Rohit Sharma) + * Fix: Enable `richtext` template tag to convert lazy translation values (Benjamin Bach) 6.1 (xx.xx.xxxx) - IN DEVELOPMENT diff --git a/docs/releases/6.2.md b/docs/releases/6.2.md index f8c59f6e652..eeea2337566 100644 --- a/docs/releases/6.2.md +++ b/docs/releases/6.2.md @@ -21,6 +21,7 @@ depth: 1 ### Bug fixes * Make `WAGTAILIMAGES_CHOOSER_PAGE_SIZE` setting functional again (Rohit Sharma) + * Enable `richtext` template tag to convert lazy translation values (Benjamin Bach) ### Documentation diff --git a/wagtail/templatetags/wagtailcore_tags.py b/wagtail/templatetags/wagtailcore_tags.py index 8bf37965c05..a8fdbd81020 100644 --- a/wagtail/templatetags/wagtailcore_tags.py +++ b/wagtail/templatetags/wagtailcore_tags.py @@ -3,6 +3,7 @@ from django.template.defaulttags import token_kwargs from django.template.loader import render_to_string from django.utils.encoding import force_str +from django.utils.functional import Promise from django.utils.html import conditional_escape from wagtail import VERSION, __version__ @@ -120,6 +121,8 @@ def richtext(value): elif value is None: html = "" else: + if isinstance(value, Promise): + value = str(value) if isinstance(value, str): html = expand_db_html(value) else: diff --git a/wagtail/tests/tests.py b/wagtail/tests/tests.py index f9c91eb6909..346f56f6ccc 100644 --- a/wagtail/tests/tests.py +++ b/wagtail/tests/tests.py @@ -9,6 +9,7 @@ from django.test.utils import override_settings from django.urls.exceptions import NoReverseMatch from django.utils.safestring import SafeString +from django.utils.translation import gettext_lazy from wagtail.coreutils import ( get_dummy_request, @@ -536,6 +537,10 @@ def test_call_with_text(self): self.assertEqual(result, "Hello world!") self.assertIsInstance(result, SafeString) + def test_call_with_lazy(self): + result = richtext(gettext_lazy("test")) + self.assertEqual(result, "test") + def test_call_with_none(self): result = richtext(None) self.assertEqual(result, "")