8000 bpo-18319: gettext() can retrieve a message even if a plural form exists by gbassiere · Pull Request #19869 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Issue #18319: gettext() can retrieve a message even if a plural form …
…exists
  • Loading branch information
gbassiere committed May 2, 2020
commit 9be6e67d549134e7753f2ccf169aebd26af24818
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:
Copy link
Contributor
@taleinat taleinat Oct 18, 2020

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
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
0