Skip to content

Commit

Permalink
[3.12] bpo-18319: gettext() can retrieve a message even if a plural f…
Browse files Browse the repository at this point in the history
…orm exists (GH-19869) (#107108)

(cherry picked from commit 5463252)

Co-authored-by: Gilles Bassière <gbassiere@gmail.com>
  • Loading branch information
miss-islington and gbassiere committed Jul 23, 2023
1 parent af95a1d commit bd72fb1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Lib/gettext.py
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_gettext.py
Expand Up @@ -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
Expand All @@ -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
Expand Down
@@ -0,0 +1,2 @@
Ensure `gettext(msg)` retrieve translations even if a plural form exists. In
other words: `gettext(msg) == ngettext(msg, '', 1)`.

0 comments on commit bd72fb1

Please sign in to comment.