Skip to content

Commit

Permalink
The html_lang Jinja2 filter should set the dir HTML attribute (#2220)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Nov 18, 2024
1 parent 4efb248 commit 41bedb9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
24 changes: 23 additions & 1 deletion betty/jinja2/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
UNDETERMINED_LOCALE,
SPECIAL_LOCALES,
)
from betty.locale.error import LocaleError
from betty.locale.localized import Localized, negotiate_localizeds, LocalizedStr
from betty.media_type import MediaType
from betty.media_type.media_types import HTML, SVG
Expand Down Expand Up @@ -117,6 +118,12 @@ def filter_localize(
return localizable.localize(context_localizer(context))


_CHARACTER_ORDER_TO_HTML_LANG_MAP = {
"left-to-right": "ltr",
"right-to-left": "rtl",
}


@pass_context
def filter_html_lang(
context: Context,
Expand All @@ -130,7 +137,22 @@ def filter_html_lang(
localizer = context_localizer(context)
result: str | Markup = localized
if localized.locale != localizer.locale:
result = f'<span lang="{localized.locale}">{localized}</span>'
localizer_locale_data = get_data(localizer.locale)
localizer_dir = _CHARACTER_ORDER_TO_HTML_LANG_MAP[
localizer_locale_data.character_order
]
try:
localized_locale_data = get_data(localized.locale)
except LocaleError:
localized_dir = "auto"
else:
localized_dir = _CHARACTER_ORDER_TO_HTML_LANG_MAP[
localized_locale_data.character_order
]
dir_attribute = (
f' dir="{localized_dir}"' if localized_dir != localizer_dir else ""
)
result = f'<span lang="{localized.locale}"{dir_attribute}>{localized}</span>'
if context.eval_ctx.autoescape:
result = Markup(result)
return result
Expand Down
20 changes: 13 additions & 7 deletions betty/tests/jinja2/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
UNDETERMINED_LOCALE,
MULTIPLE_LOCALES,
UNCODED_LOCALE,
DEFAULT_LOCALE,
)
from betty.locale.localizable import plain
from betty.locale.localized import Localized, LocalizedStr
Expand Down Expand Up @@ -592,16 +591,22 @@ async def test(self) -> None:

class TestFilterHtmlLang(TemplateStringTestBase):
@pytest.mark.parametrize(
("expected", "autoescape", "localized_locale"),
("expected", "autoescape", "localized_locale", "localizer_locale"),
[
("Hallo, wereld!", True, DEFAULT_LOCALE),
("Hallo, wereld!", False, DEFAULT_LOCALE),
('<span lang="nl">Hallo, wereld!</span>', True, "nl"),
('<span lang="nl">Hallo, wereld!</span>', False, "nl"),
("Hallo, wereld!", True, "nl", "nl"),
("Hallo, wereld!", False, "nl", "nl"),
('<span lang="nl">Hallo, wereld!</span>', True, "nl", "en"),
('<span lang="nl">Hallo, wereld!</span>', False, "nl", "en"),
('<span lang="nl" dir="ltr">Hallo, wereld!</span>', True, "nl", "ar"),
('<span lang="nl" dir="ltr">Hallo, wereld!</span>', False, "nl", "ar"),
],
)
async def test(
self, expected: str, autoescape: bool, localized_locale: str
self,
expected: str,
autoescape: bool,
localized_locale: str,
localizer_locale: str,
) -> None:
template = "{{ localized | html_lang }}"
localized = LocalizedStr("Hallo, wereld!", locale=localized_locale)
Expand All @@ -611,6 +616,7 @@ async def test(
"localized": localized,
},
autoescape=autoescape,
locale=localizer_locale,
) as (actual, _):
assert actual == expected

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Test(TemplateFileTestBase):
("expected", "data", "locale"),
[
(
'<span><a href="/place/P0/index.html"><span lang="und">The Place</span></a></span>',
'<span><a href="/place/P0/index.html"><span lang="und" dir="auto">The Place</span></a></span>',
{
"entity": Place(
id="P0",
Expand Down Expand Up @@ -54,7 +54,7 @@ class Test(TemplateFileTestBase):
"nl",
),
(
'<span><span lang="und">The Place</span></span>',
'<span><span lang="und" dir="auto">The Place</span></span>',
{
"entity": Place(
id="P0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def test_with_enclosing_place_without_place_context(self) -> None:
names=[Name("The All-enclosing Place")],
)
Enclosure(enclosee=enclosing_place, encloser=all_enclosing_place)
expected = '<div class="meta">in <span><a href="/place/P1/index.html"><span lang="und">The Enclosing Place</span></a></span>, <span><a href="/place/P2/index.html"><span lang="und">The All-enclosing Place</span></a></span></div>'
expected = '<div class="meta">in <span><a href="/place/P1/index.html"><span lang="und" dir="auto">The Enclosing Place</span></a></span>, <span><a href="/place/P2/index.html"><span lang="und" dir="auto">The All-enclosing Place</span></a></span></div>'
async with self.assert_template_file(
data={
"entity": place,
Expand All @@ -61,7 +61,7 @@ async def test_with_enclosing_place_with_matching_place_context(self) -> None:
names=[Name("The All-enclosing Place")],
)
Enclosure(enclosee=enclosing_place, encloser=all_enclosing_place)
expected = '<div class="meta">in <span><a href="/place/P1/index.html"><span lang="und">The Enclosing Place</span></a></span></div>'
expected = '<div class="meta">in <span><a href="/place/P1/index.html"><span lang="und" dir="auto">The Enclosing Place</span></a></span></div>'
async with self.assert_template_file(
data={
"entity": place,
Expand Down Expand Up @@ -89,7 +89,7 @@ async def test_with_enclosing_place_with_non_matching_place_context(self) -> Non
id="P999",
names=[Name("Far Far Away")],
)
expected = '<div class="meta">in <span><a href="/place/P1/index.html"><span lang="und">The Enclosing Place</span></a></span>, <span><a href="/place/P2/index.html"><span lang="und">The All-enclosing Place</span></a></span></div>'
expected = '<div class="meta">in <span><a href="/place/P1/index.html"><span lang="und" dir="auto">The Enclosing Place</span></a></span>, <span><a href="/place/P2/index.html"><span lang="und" dir="auto">The All-enclosing Place</span></a></span></div>'
async with self.assert_template_file(
data={
"entity": place,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def test_with_place(self) -> None:
id="P0",
names=[Name("The Place")],
)
expected = 'in <span><a href="/place/P0/index.html"><span lang="und">The Place</span></a></span>'
expected = 'in <span><a href="/place/P0/index.html"><span lang="und" dir="auto">The Place</span></a></span>'
async with self.assert_template_file(
data={
"event": event,
Expand Down Expand Up @@ -76,7 +76,7 @@ async def test_with_date_and_place(self) -> None:
id="P0",
names=[Name("The Place")],
)
expected = '1970 in <span><a href="/place/P0/index.html"><span lang="und">The Place</span></a></span>'
expected = '1970 in <span><a href="/place/P0/index.html"><span lang="und" dir="auto">The Place</span></a></span>'
async with self.assert_template_file(
data={
"event": event,
Expand Down Expand Up @@ -105,7 +105,7 @@ async def test_embedded(self) -> None:
names=[Name("The Place")],
)
event.citations.add(Citation(source=Source(name="The Source")))
expected = '1970 in <span><span lang="und">The Place</span></span>'
expected = '1970 in <span><span lang="und" dir="auto">The Place</span></span>'
async with self.assert_template_file(
data={
"event": event,
Expand Down

0 comments on commit 41bedb9

Please sign in to comment.