bpo-18319: gettext() can retrieve a message even if a plural form exists#19869
bpo-18319: gettext() can retrieve a message even if a plural form exists#19869ambv merged 4 commits intopython:mainfrom
Conversation
| return message | ||
| try: | ||
| # if `message` has plural forms | ||
| tmsg = self._catalog[(message, self.plural(1))] |
There was a problem hiding this comment.
| tmsg = self._catalog[(message, self.plural(1))] | |
| return self._catalog[(message, self.plural(1))] |
| if self._fallback: | ||
| return self._fallback.gettext(message) | ||
| return message | ||
| try: |
There was a problem hiding this comment.
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)|
Just noticed this annoying bug as well (via babel). It's impossible to work around without starting to mess around with message contexts (and I should NOT need to add garbage to my code (and pot files) because of this), so it would be great if this fix could get merged.. |
|
Should this logic be also added to |
|
Thanks @gbassiere for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11. |
|
Thanks @gbassiere for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12. |
|
GH-107107 is a backport of this pull request to the 3.11 branch. |
…sts (pythonGH-19869) (cherry picked from commit 5463252) Co-authored-by: Gilles Bassière <gbassiere@gmail.com>
…sts (pythonGH-19869) (cherry picked from commit 5463252) Co-authored-by: Gilles Bassière <gbassiere@gmail.com>
After python/cpython#19869 PR merged gettext() can lookup plural() which is not set when constructor argument (file handler) is unspecified

Let's say you have this in your code:
and elsewhere this :
GNU gettext tools extracts these as the same unique message and sensibly creates a pluralized entry in the PO file.
In the Python gettext module, when the MO file is parsed, a singular 'egg' will be stored as:
whereas a pluralized 'egg' is stored as:
As a consequence,
gettext('egg')fail to retrieve translations and you have to resort tongettext('egg', 'eggs', 1)which is odd.My suggestion is simply to modify
gettext()so that is looks forcatalog[('egg', 0)]ifcatalog['egg']didn't match.https://bugs.python.org/issue18319