[Python-checkins] [3.12] gh-62519: Make pgettext search plurals when translation is not found (GH-107118) (GH-107134)
serhiy-storchaka
webhook-mailer at python.org
Tue Jul 25 14:49:03 EDT 2023
https://github.com/python/cpython/commit/11d86c5c339abd52d1ab5ce0af4d25c44898d046
commit: 11d86c5c339abd52d1ab5ce0af4d25c44898d046
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2023-07-25T21:48:59+03:00
summary:
[3.12] gh-62519: Make pgettext search plurals when translation is not found (GH-107118) (GH-107134)
(cherry picked from commit b3c34e55c053846beb35f5e4253ef237b3494bd0)
Co-authored-by: Tomas R <tomas.roun8 at gmail.com>
Co-authored-by: Łukasz Langa <lukasz at langa.pl>
files:
A Misc/NEWS.d/next/Library/2023-07-23-12-26-23.gh-issue-62519.w8-81X.rst
M Lib/gettext.py
M Lib/test/test_gettext.py
diff --git a/Lib/gettext.py b/Lib/gettext.py
index cc938e40028c2..b72b15f82d435 100644
--- a/Lib/gettext.py
+++ b/Lib/gettext.py
@@ -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)
diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py
index aa3520d2c142e..8430fc234d00e 100644
--- a/Lib/test/test_gettext.py
+++ b/Lib/test/test_gettext.py
@@ -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
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2023-07-23-12-26-23.gh-issue-62519.w8-81X.rst b/Misc/NEWS.d/next/Library/2023-07-23-12-26-23.gh-issue-62519.w8-81X.rst
new file mode 100644
index 0000000000000..96e2a3dcc24fb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-07-23-12-26-23.gh-issue-62519.w8-81X.rst
@@ -0,0 +1,2 @@
+Make :func:`gettext.pgettext` search plural definitions when
+translation is not found.
More information about the Python-checkins
mailing list