From: Tomas R Date: Thu, 11 Jul 2024 06:11:04 +0000 (+0200) Subject: Make pgettext search plurals when translation is not found (#1085) X-Git-Tag: v2.16.0~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8de07f3aa297059dee69a5e9c7313677c2bc1e9c;p=thirdparty%2Fbabel.git Make pgettext search plurals when translation is not found (#1085) pgettext can now find the following translation when using `pgettext("ctx", "foo")`: ``` msgctxt "ctx" msgid "foo" msgid_plural "foos" msgstr[0] "foo translated" ``` The upstream CPython PR is https://github.com/python/cpython/pull/107118 --- diff --git a/babel/support.py b/babel/support.py index e6ee3780..7dcda5c6 100644 --- a/babel/support.py +++ b/babel/support.py @@ -466,10 +466,12 @@ class NullTranslations(gettext.NullTranslations): missing = object() tmsg = self._catalog.get(ctxt_msg_id, missing) if tmsg is missing: - if self._fallback: - return self._fallback.pgettext(context, message) - return message - return tmsg + tmsg = self._catalog.get((ctxt_msg_id, self.plural(1)), missing) + if tmsg is not missing: + return tmsg + if self._fallback: + return self._fallback.pgettext(context, message) + return message def lpgettext(self, context: str, message: str) -> str | bytes | object: """Equivalent to ``pgettext()``, but the translation is returned in the diff --git a/tests/test_support.py b/tests/test_support.py index 12627205..366c2941 100644 --- a/tests/test_support.py +++ b/tests/test_support.py @@ -71,6 +71,14 @@ class TranslationsTestCase(unittest.TestCase): self.assertEqualTypeToo('Voh', self.translations.gettext('foo')) self.assertEqualTypeToo('VohCTX', self.translations.pgettext('foo', 'foo')) + self.assertEqualTypeToo('VohCTX1', self.translations.pgettext('foo', + 'foo1')) + + def test_pgettext_fallback(self): + fallback = self.translations._fallback + self.translations._fallback = support.NullTranslations() + assert self.translations.pgettext('foo', 'bar') == 'bar' + self.translations._fallback = fallback def test_upgettext(self): self.assertEqualTypeToo('Voh', self.translations.ugettext('foo'))