From 6e83f27ae37d677c160e64f9372e8847b1c552a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Furga=C5=82a?= <83299832+00200200@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:27:14 +0200 Subject: [PATCH] Fix contextual plural lookup in upgettext --- babel/support.py | 2 ++ tests/test_support_translations.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/babel/support.py b/babel/support.py index 8cc2492e8..63966179a 100644 --- a/babel/support.py +++ b/babel/support.py @@ -605,6 +605,8 @@ def upgettext(self, context: str, message: str) -> str: ctxt_message_id = self.CONTEXT_ENCODING % (context, message) missing = object() tmsg = self._catalog.get(ctxt_message_id, missing) + if tmsg is missing: + tmsg = self._catalog.get((ctxt_message_id, self.plural(1)), missing) if tmsg is missing: if self._fallback: return self._fallback.upgettext(context, message) diff --git a/tests/test_support_translations.py b/tests/test_support_translations.py index bf2eb2b98..5e0a780f1 100644 --- a/tests/test_support_translations.py +++ b/tests/test_support_translations.py @@ -94,6 +94,25 @@ def test_upgettext(translations): assert_equal_type_too('VohCTX', translations.upgettext('foo', 'foo')) +@pytest.mark.parametrize('use_fallback', [False, True]) +def test_upgettext_plural_entry(translations, use_fallback): + if use_fallback: + primary = support.Translations() + primary.add_fallback(translations) + translations = primary + + expected = translations.pgettext('foo', 'foo1') + assert_equal_type_too('VohCTX1', expected) + assert_equal_type_too(expected, translations.upgettext('foo', 'foo1')) + + +@pytest.mark.parametrize('method', ['udpgettext', 'dupgettext']) +def test_upgettext_plural_entry_in_domain(translations, method): + expected = translations.dpgettext('messages1', 'foo', 'foo1') + assert_equal_type_too('VohCTXD1', expected) + assert_equal_type_too(expected, getattr(translations, method)('messages1', 'foo', 'foo1')) + + @pytest.mark.skipif(SKIP_LGETTEXT, reason='lgettext is deprecated') def test_lpgettext(translations): assert_equal_type_too(b'Voh', translations.lgettext('foo'))