diff options
author | Tomas R <tomas.roun8@gmail.com> | 2023-07-23 14:08:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-23 14:08:28 (GMT) |
commit | b3c34e55c053846beb35f5e4253ef237b3494bd0 (patch) | |
tree | 350c648bea1175063306096b75c36b742e496b1d | |
parent | b273837fea129df8477da557ad3cb23a0ed12c20 (diff) | |
download | cpython-b3c34e55c053846beb35f5e4253ef237b3494bd0.zip cpython-b3c34e55c053846beb35f5e4253ef237b3494bd0.tar.gz cpython-b3c34e55c053846beb35f5e4253ef237b3494bd0.tar.bz2 |
gh-62519: Make pgettext search plurals when translation is not found (#107118)
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
-rw-r--r-- | Lib/gettext.py | 10 | ||||
-rw-r--r-- | Lib/test/test_gettext.py | 4 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2023-07-23-12-26-23.gh-issue-62519.w8-81X.rst | 2 |
3 files changed, 12 insertions, 4 deletions
diff --git a/Lib/gettext.py b/Lib/gettext.py index cc938e40..b72b15f 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -446,10 +446,12 @@ class GNUTranslations(NullTranslations): 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 aa3520d..8430fc2 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -331,6 +331,8 @@ class PluralFormsTestCase(GettextBaseTest): 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 @@ class PluralFormsTestCase(GettextBaseTest): 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 0000000..96e2a3d --- /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. |