Skip to content

Commit c1c3f0b

Browse files
[3.11] bpo-18319: gettext() can retrieve a message even if a plural form exists (GH-19869) (GH-107107)
(cherry picked from commit 5463252) Co-authored-by: Gilles Bassière <[email protected]>
1 parent 26137e2 commit c1c3f0b

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Lib/gettext.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,12 @@ def gettext(self, message):
422422
missing = object()
423423
tmsg = self._catalog.get(message, missing)
424424
if tmsg is missing:
425-
if self._fallback:
426-
return self._fallback.gettext(message)
427-
return message
428-
return tmsg
425+
tmsg = self._catalog.get((message, self.plural(1)), missing)
426+
if tmsg is not missing:
427+
return tmsg
428+
if self._fallback:
429+
return self._fallback.gettext(message)
430+
return message
429431

430432
def ngettext(self, msgid1, msgid2, n):
431433
try:

Lib/test/test_gettext.py

+4
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ def test_plural_forms1(self):
320320
eq(x, 'Hay %s fichero')
321321
x = gettext.ngettext('There is %s file', 'There are %s files', 2)
322322
eq(x, 'Hay %s ficheros')
323+
x = gettext.gettext('There is %s file')
324+
eq(x, 'Hay %s fichero')
323325

324326
def test_plural_context_forms1(self):
325327
eq = self.assertEqual
@@ -340,6 +342,8 @@ def test_plural_forms2(self):
340342
eq(x, 'Hay %s fichero')
341343
x = t.ngettext('There is %s file', 'There are %s files', 2)
342344
eq(x, 'Hay %s ficheros')
345+
x = t.gettext('There is %s file')
346+
eq(x, 'Hay %s fichero')
343347

344348
def test_plural_context_forms2(self):
345349
eq = self.assertEqual
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensure ``gettext(msg)`` retrieve translations even if a plural form exists. In
2+
other words: ``gettext(msg) == ngettext(msg, '', 1)``.

0 commit comments

Comments
 (0)