diff --git a/tests/unit/python/utils/i18n/test_catalog.py b/tests/unit/python/utils/i18n/test_catalog.py index 7224f51e67..40c62d2700 100644 --- a/tests/unit/python/utils/i18n/test_catalog.py +++ b/tests/unit/python/utils/i18n/test_catalog.py @@ -12,6 +12,25 @@ from utils.i18n.catalog import ( from utils.i18n.translate import pending +class TestBuildTemplate(unittest.TestCase): + def test_a_message_that_is_only_a_name_never_enters_the_template(self) -> None: + template = build_template( + [("a", "Mastodon"), ("b", "Mastodon is a federated network.")], "core" + ) + + self.assertEqual( + [message.id for message in template if message.id], + ["Mastodon is a federated network."], + "core never passes through sphinx, so without this the docs domain " + "withholds a name its own catalog still carries", + ) + + def test_a_message_without_a_word_never_enters_the_template(self) -> None: + template = build_template([("a", "`x`"), ("b", "Real prose.")], "core") + + self.assertEqual([message.id for message in template if message.id], ["Real prose."]) + + class TestMerge(unittest.TestCase): def test_changed_source_comes_back_empty_for_the_translator(self): catalog = merge(build_template([("a", "Hello world")], "core"), None, "de") diff --git a/utils/i18n/catalog.py b/utils/i18n/catalog.py index 333fb0ffb8..9aab2448a7 100644 --- a/utils/i18n/catalog.py +++ b/utils/i18n/catalog.py @@ -11,6 +11,7 @@ from babel.messages.catalog import Catalog from babel.messages.pofile import read_po, write_po from utils.i18n.placeholders import carries_placeholder +from utils.i18n.untranslatable import untranslatable from utils.software import ( SOFTWARE_AUTHOR, SOFTWARE_CONTACT, @@ -65,7 +66,12 @@ def new_catalog(domain: str, code: str | None = None) -> Catalog: def build_template(messages: Iterable[tuple[str, str]], domain: str) -> Catalog: - """Return a template catalog holding ``messages``. + """Return a template catalog holding the translatable part of ``messages``. + + This is the ``core`` domain's counterpart to the sphinx transform the + ``docs`` domain carries: ``core`` never passes through sphinx, so a message + that is nothing but a name would otherwise reach its catalog while the same + message is withheld from the other domain. Args: messages: ``(msgctxt, msgid)`` pairs; ``msgctxt`` may be ``None``. @@ -73,7 +79,8 @@ def build_template(messages: Iterable[tuple[str, str]], domain: str) -> Catalog: """ template = new_catalog(domain) for context, msgid in messages: - template.add(msgid, context=context) + if not untranslatable(msgid): + template.add(msgid, context=context) return template