[Python-checkins] bpo-18319: gettext() can retrieve a message even if a plural form exists (#19869)

ambv webhook-mailer at python.org
Sun Jul 23 05:59:23 EDT 2023


https://github.com/python/cpython/commit/54632528eeba841e4a8cc95ecbd84c9aca8eef57
commit: 54632528eeba841e4a8cc95ecbd84c9aca8eef57
branch: main
author: Gilles Bassière <gbassiere at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2023年07月23日T11:59:19+02:00
summary:
bpo-18319: gettext() can retrieve a message even if a plural form exists (#19869)
files:
A Misc/NEWS.d/next/Library/2020-05-03-00-33-15.bpo-18319.faPTlx.rst
M Lib/gettext.py
M Lib/test/test_gettext.py
diff --git a/Lib/gettext.py b/Lib/gettext.py
index 6c5ec4e517f63..cc938e40028c2 100644
--- a/Lib/gettext.py
+++ b/Lib/gettext.py
@@ -422,10 +422,12 @@ 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
- 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:
diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py
index 1608d1b18e98f..aa3520d2c142e 100644
--- a/Lib/test/test_gettext.py
+++ b/Lib/test/test_gettext.py
@@ -320,6 +320,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
@@ -338,6 +340,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
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 0000000000000..a1a4cf6d63725
--- /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)`.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /