Skip to content

Commit

Permalink
pythongh-62519: Make pgettext search plurals when translation is not …
Browse files Browse the repository at this point in the history
…found (python#107118)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
  • Loading branch information
2 people authored and mementum committed Jul 23, 2023
1 parent c3fb34e commit 307e1e2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,12 @@ def pgettext(self, context, message):
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 npgettext(self, context, msgid1, msgid2, n):
ctxt_msg_id = self.CONTEXT % (context, msgid1)
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ def test_plural_context_forms1(self):
x = gettext.npgettext('With context',
'There is %s file', 'There are %s files', 2)
eq(x, 'Hay %s ficheros (context)')
x = gettext.pgettext('With context', 'There is %s file')
eq(x, 'Hay %s fichero (context)')

def test_plural_forms2(self):
eq = self.assertEqual
Expand All @@ -353,6 +355,8 @@ def test_plural_context_forms2(self):
x = t.npgettext('With context',
'There is %s file', 'There are %s files', 2)
eq(x, 'Hay %s ficheros (context)')
x = gettext.pgettext('With context', 'There is %s file')
eq(x, 'Hay %s fichero (context)')

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

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

0 comments on commit 307e1e2

Please sign in to comment.