From bda6826f76e0230f48908bce84bc91ac7248019e Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 24 Nov 2026 08:49:32 +0300 Subject: [PATCH] test(i18n): Django loads each language's django.mo and returns its translations --- webvirtcloud/tests/test_catalogs.py | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 webvirtcloud/tests/test_catalogs.py diff --git a/webvirtcloud/tests/test_catalogs.py b/webvirtcloud/tests/test_catalogs.py new file mode 100644 index 00000000..de7f76c8 --- /dev/null +++ b/webvirtcloud/tests/test_catalogs.py @@ -0,0 +1,33 @@ +"""Each translated language of LANGUAGES gets its locale/ django.mo: Django +finds the catalog for the language code (zh-hans reads locale/zh) and every +message comes out as the catalog translates it, none covered by another +catalog.""" + +import gettext + +from django.conf import settings +from django.test import SimpleTestCase +from django.utils import translation + +CATALOG_DIRS = {"tr": "tr", "zh-hans": "zh"} + + +class CatalogTests(SimpleTestCase): + def test_every_language_has_a_catalog(self): + self.assertEqual({code for code, _ in settings.LANGUAGES} - {"en"}, set(CATALOG_DIRS)) + + def test_every_message_is_translated_as_its_catalog_says(self): + for code, directory in CATALOG_DIRS.items(): + path = settings.BASE_DIR / "locale" / directory / "LC_MESSAGES" / "django.mo" + with open(path, "rb") as mo: + catalog = gettext.GNUTranslations(mo)._catalog + messages = {key: text for key, text in catalog.items() if isinstance(key, str) and key} + self.assertGreater(len(messages), 100, path) + with translation.override(code): + for key, text in messages.items(): + context, _, message = key.rpartition("\x04") + with self.subTest(language=code, message=message): + if context: + self.assertEqual(translation.pgettext(context, message), text) + else: + self.assertEqual(translation.gettext(message), text)