Skip to content

Commit b3c34e5

Browse files
tomasr8ambv
andauthored
gh-62519: Make pgettext search plurals when translation is not found (#107118)
Co-authored-by: Łukasz Langa <[email protected]>
1 parent b273837 commit b3c34e5

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Lib/gettext.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,12 @@ def pgettext(self, context, message):
446446
missing = object()
447447
tmsg = self._catalog.get(ctxt_msg_id, missing)
448448
if tmsg is missing:
449-
if self._fallback:
450-
return self._fallback.pgettext(context, message)
451-
return message
452-
return tmsg
449+
tmsg = self._catalog.get((ctxt_msg_id, self.plural(1)), missing)
450+
if tmsg is not missing:
451+
return tmsg
452+
if self._fallback:
453+
return self._fallback.pgettext(context, message)
454+
return message
453455

454456
def npgettext(self, context, msgid1, msgid2, n):
455457
ctxt_msg_id = self.CONTEXT % (context, msgid1)

Lib/test/test_gettext.py

+4
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ def test_plural_context_forms1(self):
331331
x = gettext.npgettext('With context',
332332
'There is %s file', 'There are %s files', 2)
333333
eq(x, 'Hay %s ficheros (context)')
334+
x = gettext.pgettext('With context', 'There is %s file')
335+
eq(x, 'Hay %s fichero (context)')
334336

335337
def test_plural_forms2(self):
336338
eq = self.assertEqual
@@ -353,6 +355,8 @@ def test_plural_context_forms2(self):
353355
x = t.npgettext('With context',
354356
'There is %s file', 'There are %s files', 2)
355357
eq(x, 'Hay %s ficheros (context)')
358+
x = gettext.pgettext('With context', 'There is %s file')
359+
eq(x, 'Hay %s fichero (context)')
356360

357361
# Examples from http://www.gnu.org/software/gettext/manual/gettext.html
358362

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make :func:`gettext.pgettext` search plural definitions when
2+
translation is not found.

0 commit comments

Comments
 (0)