Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

@taleinat taleinat Oct 18, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest simply adding the following before the existing if tmsg is missing: ... block:

        if tmsg is missing:
            tmsg = self._catalog.get((message, self.plural(1)), missing)

# if `message` has plural forms
tmsg = self._catalog[(message, self.plural(1))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tmsg = self._catalog[(message, self.plural(1))]
return 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):
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure `gettext(msg)` retrieve translations even if a plural form exists. In
other words: `gettext(msg) == ngettext(msg, '', 1)`.