diff --git a/tests/unit/python/utils/i18n/test_untranslatable.py b/tests/unit/python/utils/i18n/test_untranslatable.py index 7ac292d300..8cadb9ada2 100644 --- a/tests/unit/python/utils/i18n/test_untranslatable.py +++ b/tests/unit/python/utils/i18n/test_untranslatable.py @@ -33,6 +33,15 @@ class TestUntranslatable(unittest.TestCase): with self.subTest(marked): self.assertTrue(untranslatable(marked)) + def test_a_name_carrying_a_digit_survives_the_stripping(self) -> None: + for written in ("n8n", "**n8n**", "Mini-QR"): + with self.subTest(written): + self.assertTrue( + untranslatable(written), + "comparing against the prose left 'nn', because masking " + "treats the digit as a span of its own", + ) + def test_the_name_is_matched_whatever_case_the_page_wrote_it_in(self) -> None: self.assertTrue( untranslatable("**mailu**"), @@ -40,6 +49,31 @@ class TestUntranslatable(unittest.TestCase): "product is Mailu either way", ) + def test_a_link_whose_text_is_the_name_is_withheld(self) -> None: + for written in ( + "[Baserow](roles/web-app-baserow/)", + "[Mastodon](https://joinmastodon.org)", + ): + with self.subTest(written): + self.assertTrue(untranslatable(written)) + + def test_an_emoji_behind_the_name_does_not_hide_it(self) -> None: + for written in ("Git 🔐", "Claude Code 🤖"): + with self.subTest(written): + self.assertTrue(untranslatable(written)) + + def test_a_message_saying_more_than_the_name_is_translated(self) -> None: + for written in ("Redis ``cache``", "Redis `queue`", "[Documentation](docs/)"): + with self.subTest(written): + self.assertFalse( + untranslatable(written), + "stripping every protected span instead of the decoration " + "withheld 131 messages that say more than a name", + ) + + def test_a_link_inside_a_sentence_is_translated(self) -> None: + self.assertFalse(untranslatable("See [Mastodon](https://x) and more.")) + def test_a_marked_up_description_is_still_translated(self) -> None: self.assertFalse(untranslatable("**Cleanup Disc Space**")) diff --git a/utils/i18n/untranslatable.py b/utils/i18n/untranslatable.py index 5be7712fdb..9e8a675ae1 100644 --- a/utils/i18n/untranslatable.py +++ b/utils/i18n/untranslatable.py @@ -17,13 +17,41 @@ the catalogs withhold is the same message the client would have refused to send. from __future__ import annotations import re +import unicodedata -from utils.i18n.placeholders import has_words, prose +from utils.i18n.placeholders import has_words from utils.meta.authors import author_names from utils.meta.role.brands import brand_titles from utils.software import SOFTWARE_NAME URL = re.compile(r"^(?:https?://|www\.|mailto:|tel:)\S+$") +LINK = re.compile(r"^!?\[(?P[^\]]*)\]\([^)]*\)$") +EMPHASIS = "*_`~ \t" +DECORATION = {"So", "Sk", "Cf", "Mn"} + + +def _bare(text: str) -> str: + """Return ``text`` without the decoration a page wraps a name in. + + A heading writes the name with an emoji behind it and a table cell links it + to the role that deploys it, so ``Git 🔐`` and ``[Baserow](roles/web-app- + baserow/)`` are the product under decoration rather than a phrase about it. + + Only emphasis, a surrounding link and trailing symbols are removed. Taking + the prose instead, as an earlier version did, drops every protected span: + ``Redis ``cache``` then reads as ``Redis`` and 131 messages that say more + than a name were withheld. + + Args: + text: a source message. + """ + bare = text.strip().strip(EMPHASIS) + link = LINK.match(bare) + if link: + bare = link.group("text").strip().strip(EMPHASIS) + while bare and unicodedata.category(bare[-1]) in DECORATION: + bare = bare[:-1].strip(EMPHASIS) + return bare def _known() -> frozenset[str]: @@ -35,22 +63,20 @@ def _known() -> frozenset[str]: def is_name(text: str) -> bool: """Whether ``text`` is nothing but a brand, a credited person or a URL. - The comparison runs against the prose, so the markdown a heading wraps the - name in falls away first: ``**Mastodon**`` is the product under emphasis, - not a phrase about it. Case is ignored because a page writes the name the - way its sentence needs it, and ``**mailu**`` is still Mailu. + The decoration a page wraps the name in is removed first; see :func:`_bare`. + Case is ignored because a page writes the name the way its sentence needs + it, and ``**mailu**`` is still Mailu. - An address is matched before that, on the raw text: masking protects the - address of ``mailto:a@b.c`` but leaves the scheme behind, so its prose reads - as a word. + An address is matched before that, on the raw text, because a URL carries + the characters the stripping removes. Args: text: a source message. """ if URL.match(text.strip()): return True - stripped = prose(text).strip() - return bool(stripped) and stripped.lower() in _known() + bare = _bare(text) + return bool(bare) and bare.lower() in _known() def untranslatable(text: str) -> bool: