Skip to content

gh-64243: Implement locale.getlocale fall back in gettext.find #131477

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Clean up confusion
  • Loading branch information
StanFromIreland committed Jun 19, 2025
commit ff7d10a36a27417f6356d0453603c324f85f49ea
4 changes: 2 additions & 2 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ def find(domain, localedir=None, languages=None, all=False):
languages = []
if val := os.environ.get('LANGUAGE'):
languages = val.split(':')
elif os.name == 'posix' and (loc := locale.setlocale(locale.LC_MESSAGES)):
languages = loc.split(':')
elif (loc := locale.getlocale()) != (None, None):
languages = [".".join(filter(None, loc))]
else:
for envar in ('LC_ALL', 'LC_MESSAGES', 'LANG'):
val = os.environ.get(envar)
Expand Down
20 changes: 8 additions & 12 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ def _for_all_vars(self, mo_file, locale, expected=True):
self.assertIsNone(result)
self.env.unset(var)

@unittest.mock.patch("locale.setlocale", return_value='')
@unittest.mock.patch("locale.getlocale", return_value=(None, None))
def test_find_with_env_vars(self, patch_getlocale):
# test that find correctly finds the environment variables
# when languages are not supplied
Expand All @@ -769,30 +769,26 @@ def test_encoding_not_ignored(self, patch_expand_lang):
patch_expand_lang.assert_any_call('ga_IE.UTF-8')
self.env.unset('LANGUAGE')

@unittest.skipIf(os.name != "posix", "LC_MESSAGES is posix only")
def test_find_LANGUAGE_priority(self):
self.env.set('LANGUAGE', 'ga_IE')
self.env.set('LC_ALL', 'pt_BR')
if os.name != "posix":
orig = locale.setlocale(locale.LC_MESSAGES)
self.addCleanup(lambda: locale.setlocale(locale.LC_MESSAGES, orig))
locale.setlocale(locale.LC_MESSAGES, 'pt_BR')
orig = locale.setlocale(locale.LC_ALL)
self.addCleanup(lambda: locale.setlocale(locale.LC_ALL, orig))
locale.setlocale(locale.LC_ALL, 'pt_BR')
mo_file = self.create_mo_file("ga_IE")

result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
self.assertEqual(result, mo_file)

@unittest.skipIf(os.name != "posix", "LC_MESSAGES is posix only")
def test_process_vars_override(self):
orig = locale.setlocale(locale.LC_MESSAGES)
self.addCleanup(lambda: locale.setlocale(locale.LC_MESSAGES, orig))
orig = locale.setlocale(locale.LC_ALL)
self.addCleanup(lambda: locale.setlocale(locale.LC_ALL, orig))
mo_file = self.create_mo_file("ca_ES")
for loc in ("ca_ES", "ca_ES.UTF-8", "ca_ES@euro", "ca_ES@valencia"):
locale.setlocale(locale.LC_MESSAGES, loc)
locale.setlocale(locale.LC_ALL, loc)
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
self.assertEqual(mo_file, result)
for loc in ("C", "C.UTF-8"):
locale.setlocale(locale.LC_MESSAGES, loc)
locale.setlocale(locale.LC_ALL, loc)
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
self.assertIsNone(result)

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy