summaryrefslogtreecommitdiff
path: root/tests/i18n
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2024-09-14 19:10:01 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-09-16 11:04:09 +0200
commitb579485d991da65ebe4a6cdbcab20f59f7515d3f (patch)
tree609e0e1742afeb1fecc9c6f2e3571426b295a4d7 /tests/i18n
parent1f3f0cd8cabd201063ac024cb72eea27ea8c4aa8 (diff)
Fixed #34221 -- Honored translation precedence with mixed plural forms.
Diffstat (limited to 'tests/i18n')
-rw-r--r--tests/i18n/tests.py46
1 files changed, 45 insertions, 1 deletions
diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py
index a517a2d27d..1f50ba1112 100644
--- a/tests/i18n/tests.py
+++ b/tests/i18n/tests.py
@@ -8,7 +8,7 @@ import tempfile
from contextlib import contextmanager
from importlib import import_module
from pathlib import Path
-from unittest import mock
+from unittest import mock, skipUnless
from asgiref.local import Local
@@ -17,6 +17,7 @@ from django.apps import AppConfig
from django.conf import settings
from django.conf.locale import LANG_INFO
from django.conf.urls.i18n import i18n_patterns
+from django.core.management.utils import find_command, popen_wrapper
from django.template import Context, Template
from django.test import RequestFactory, SimpleTestCase, TestCase, override_settings
from django.utils import translation
@@ -130,6 +131,49 @@ class TranslationTests(SimpleTestCase):
self.assertEqual(french._catalog[("%d singular", 0)], "%d singulier")
self.assertEqual(french._catalog[("%(num)d hour", 0)], "%(num)d heure")
+ @translation.override("fr")
+ @skipUnless(find_command("msgfmt"), "msgfmt is mandatory for this test")
+ def test_multiple_plurals_merge(self):
+ def _create_translation_from_string(content):
+ with tempfile.TemporaryDirectory() as dirname:
+ po_path = Path(dirname).joinpath("fr", "LC_MESSAGES", "django.po")
+ po_path.parent.mkdir(parents=True)
+ po_path.write_text(content)
+ errors = popen_wrapper(
+ ["msgfmt", "-o", po_path.with_suffix(".mo"), po_path]
+ )[1]
+ if errors:
+ self.fail(f"msgfmt compilation error: {errors}")
+ return gettext_module.translation(
+ domain="django",
+ localedir=dirname,
+ languages=["fr"],
+ )
+
+ french = trans_real.catalog()
+ # Merge a new translation file with different plural forms.
+ catalog1 = _create_translation_from_string(
+ 'msgid ""\n'
+ 'msgstr ""\n'
+ '"Content-Type: text/plain; charset=UTF-8\\n"\n'
+ '"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n==0 ? 1 : 2);\\n"\n'
+ 'msgid "I win"\n'
+ 'msgstr "Je perds"\n'
+ )
+ french.merge(catalog1)
+ # Merge a second translation file with plural forms from django.conf.
+ catalog2 = _create_translation_from_string(
+ 'msgid ""\n'
+ 'msgstr ""\n'
+ '"Content-Type: text/plain; charset=UTF-8\\n"\n'
+ '"Plural-Forms: Plural-Forms: nplurals=2; plural=(n > 1);\\n"\n'
+ 'msgid "I win"\n'
+ 'msgstr "Je gagne"\n'
+ )
+ french.merge(catalog2)
+ # Translations from this last one are supposed to win.
+ self.assertEqual(french.gettext("I win"), "Je gagne")
+
def test_override(self):
activate("de")
try: