]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] bpo-18319: gettext() can retrieve a message even if a plural form exists ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 16 Aug 2023 09:15:01 +0000 (02:15 -0700)
committerGitHub <noreply@github.com>
Wed, 16 Aug 2023 09:15:01 +0000 (09:15 +0000)
(cherry picked from commit 54632528eeba841e4a8cc95ecbd84c9aca8eef57)

Co-authored-by: Gilles Bassière <gbassiere@gmail.com>
Lib/gettext.py
Lib/test/test_gettext.py
Misc/NEWS.d/next/Library/2020-05-03-00-33-15.bpo-18319.faPTlx.rst [new file with mode: 0644]

index 57f1a449c28a43e56cb0a3e0b0899a0440a37004..b72b15f82d43552162cc322ca69df57f14451f1b 100644 (file)
@@ -422,10 +422,12 @@ class GNUTranslations(NullTranslations):
         missing = object()
         tmsg = self._catalog.get(message, missing)
         if tmsg is missing:
-            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:
index 7f7b51c19f35c77a302e0e5de0ea7e758dd21dc1..8430fc234d00eebc95f6c0188b68ff0d1c4c1639 100644 (file)
@@ -320,6 +320,8 @@ class PluralFormsTestCase(GettextBaseTest):
         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
@@ -340,6 +342,8 @@ class PluralFormsTestCase(GettextBaseTest):
         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
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 (file)
index 0000000..94d7cc9
--- /dev/null
@@ -0,0 +1,2 @@
+Ensure ``gettext(msg)`` retrieve translations even if a plural form exists. In
+other words: ``gettext(msg) == ngettext(msg, '', 1)``.