From 9be6e67d549134e7753f2ccf169aebd26af24818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilles=20Bassi=C3=A8re?= Date: Sat, 2 May 2020 23:24:29 +0200 Subject: [PATCH 1/3] Issue #18319: gettext() can retrieve a message even if a plural form exists --- Lib/gettext.py | 10 +++++++--- Lib/test/test_gettext.py | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Lib/gettext.py b/Lib/gettext.py index b98f501884b75a..2efeeb01d384aa 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -493,9 +493,13 @@ def gettext(self, message): missing = object() tmsg = self._catalog.get(message, missing) if tmsg is missing: - if self._fallback: - return self._fallback.gettext(message) - return message + try: + # if `message` has plural forms + tmsg = self._catalog[(message, self.plural(1))] + except KeyError: + if self._fallback: + return self._fallback.gettext(message) + return message return tmsg def ngettext(self, msgid1, msgid2, n): diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index baf300b0572495..f91c09e8ef402f 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -321,6 +321,8 @@ def test_plural_forms1(self): eq(x, 'Hay %s fichero') x = gettext.ngettext('There is %s file', 'There are %s files', 2) eq(x, 'Hay %s ficheros') + x = gettext.gettext('There is %s file') + eq(x, 'Hay %s fichero') def test_plural_context_forms1(self): eq = self.assertEqual @@ -339,6 +341,8 @@ def test_plural_forms2(self): eq(x, 'Hay %s fichero') x = t.ngettext('There is %s file', 'There are %s files', 2) eq(x, 'Hay %s ficheros') + x = t.gettext('There is %s file') + eq(x, 'Hay %s fichero') def test_plural_context_forms2(self): eq = self.assertEqual From 26b8f1c832b7e11c411ae9a3ce1954cd98da2038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilles=20Bassi=C3=A8re?= Date: Sun, 3 May 2020 00:36:21 +0200 Subject: [PATCH 2/3] Add missing Misc/NEWS.d --- .../next/Library/2020-05-03-00-33-15.bpo-18319.faPTlx.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-05-03-00-33-15.bpo-18319.faPTlx.rst diff --git a/Misc/NEWS.d/next/Library/2020-05-03-00-33-15.bpo-18319.faPTlx.rst b/Misc/NEWS.d/next/Library/2020-05-03-00-33-15.bpo-18319.faPTlx.rst new file mode 100644 index 00000000000000..a1a4cf6d63725a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-03-00-33-15.bpo-18319.faPTlx.rst @@ -0,0 +1,2 @@ +Ensure `gettext(msg)` retrieve translations even if a plural form exists. In +other words: `gettext(msg) == ngettext(msg, '', 1)`. From e120d707ac27027b5a603cdd1a26398473d9f17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Sun, 23 Jul 2023 10:07:12 +0200 Subject: [PATCH 3/3] Code golf --- Lib/gettext.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Lib/gettext.py b/Lib/gettext.py index 84e455c7b7bca1..cc938e40028c24 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -422,14 +422,12 @@ def gettext(self, message): missing = object() tmsg = self._catalog.get(message, missing) if tmsg is missing: - try: - # if `message` has plural forms - tmsg = self._catalog[(message, self.plural(1))] - except KeyError: - if self._fallback: - return self._fallback.gettext(message) - return message - return tmsg + tmsg = self._catalog.get((message, self.plural(1)), missing) + if tmsg is not missing: + return tmsg + if self._fallback: + return self._fallback.gettext(message) + return message def ngettext(self, msgid1, msgid2, n): try: