Skip to content

Commit

Permalink
Bug: Enable template tag richtext to convert lazy text strings (#11901
Browse files Browse the repository at this point in the history
)

Fixes #11900
  • Loading branch information
benjaoming authored and gasman committed Apr 29, 2024
1 parent 763c990 commit b266e54
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/releases/6.2.md
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions wagtail/templatetags/wagtailcore_tags.py
Expand Up @@ -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__
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions wagtail/tests/tests.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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, "")
Expand Down

0 comments on commit b266e54

Please sign in to comment.