Skip to content

Commit 8de07f3

Browse files
authored
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 python/cpython#107118
1 parent 75486c9 commit 8de07f3

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

babel/support.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,12 @@ def pgettext(self, context: str, message: str) -> str | object:
466466
missing = object()
467467
tmsg = self._catalog.get(ctxt_msg_id, missing)
468468
if tmsg is missing:
469-
if self._fallback:
470-
return self._fallback.pgettext(context, message)
471-
return message
472-
return tmsg
469+
tmsg = self._catalog.get((ctxt_msg_id, self.plural(1)), missing)
470+
if tmsg is not missing:
471+
return tmsg
472+
if self._fallback:
473+
return self._fallback.pgettext(context, message)
474+
return message
473475

474476
def lpgettext(self, context: str, message: str) -> str | bytes | object:
475477
"""Equivalent to ``pgettext()``, but the translation is returned in the

tests/test_support.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ def test_pgettext(self):
7171
self.assertEqualTypeToo('Voh', self.translations.gettext('foo'))
7272
self.assertEqualTypeToo('VohCTX', self.translations.pgettext('foo',
7373
'foo'))
74+
self.assertEqualTypeToo('VohCTX1', self.translations.pgettext('foo',
75+
'foo1'))
76+
77+
def test_pgettext_fallback(self):
78+
fallback = self.translations._fallback
79+
self.translations._fallback = support.NullTranslations()
80+
assert self.translations.pgettext('foo', 'bar') == 'bar'
81+
self.translations._fallback = fallback
7482

7583
def test_upgettext(self):
7684
self.assertEqualTypeToo('Voh', self.translations.ugettext('foo'))

0 commit comments

Comments
 (0)