Skip to content

Commit 06f541b

Browse files
tomasr8akx
authored andcommitted
Make pgettext search plurals when translation is not found
pgettext can now find the following translation when using `pgettext("ctx", "foo")`: msgctxt "ctx" msgid "foo" msgid_plural "foos" msgstr[0] "foo translated"
1 parent 3edf772 commit 06f541b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ 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'))
7476

7577
def test_upgettext(self):
7678
self.assertEqualTypeToo('Voh', self.translations.ugettext('foo'))

0 commit comments

Comments
 (0)