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
Next Next commit
Initial
  • Loading branch information
StanFromIreland committed Mar 19, 2025
commit fcce7bd887d9d39f45456009f2f5ad48e6a4c476
10 changes: 7 additions & 3 deletions Doc/library/gettext.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ install themselves in the built-in namespace as the function :func:`!_`.
strings, where each string is a language code.

If *localedir* is not given, then the default system locale directory is used.
[#]_ If *languages* is not given, then the following environment variables are
searched: :envvar:`LANGUAGE`, :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
:envvar:`LANG`. The first one returning a non-empty value is used for the
[#]_ If *languages* is not given, then the environment variable :envvar:`LANGUAGE`
is searched, it falls back to :func:`locale.getlocale`, which in turn falls
back to the environment variables :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
:envvar:`LANG` where the first one returning a non-empty value is used for the
*languages* variable. The environment variables should contain a colon separated
list of languages, which will be split on the colon to produce the expected list
of language code strings.
Expand All @@ -147,6 +148,9 @@ install themselves in the built-in namespace as the function :func:`!_`.
of all file names, in the order in which they appear in the languages list or
the environment variables.

.. versionchanged:: next
:func:`locale.getlocale` is used to generate *languages* if *languages* is
not provided.

.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False)

Expand Down
16 changes: 11 additions & 5 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import operator
import os
import sys
import locale


__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
Expand Down Expand Up @@ -491,11 +492,16 @@ def find(domain, localedir=None, languages=None, all=False):
localedir = _default_localedir
if languages is None:
languages = []
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
val = os.environ.get(envar)
if val:
languages = val.split(':')
break
if os.environ.get('LANGUAGE'):
languages = os.environ.get('LANGUAGE').split(',')
elif locale.getlocale() != (None, None):
languages.append(".".join(filter(None, locale.getlocale())))
else:
for envar in ('LC_ALL', 'LC_MESSAGES', 'LANG'):
val = os.environ.get(envar)
if val:
languages = val.split(':')
break
if 'C' not in languages:
languages.append('C')
# now normalize and expand the languages
Expand Down
10 changes: 9 additions & 1 deletion Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import locale
import os
import base64
import gettext
Expand Down Expand Up @@ -736,7 +737,8 @@ def create_mo_file(self, lang):
f.write(GNU_MO_DATA)
return mo_file

def test_find_with_env_vars(self):
@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
mo_file = self.create_mo_file("ga_IE")
Expand All @@ -747,6 +749,12 @@ def test_find_with_env_vars(self):
self.assertEqual(result, mo_file)
self.env.unset(var)

@unittest.mock.patch("locale.getlocale", return_value=('ga_IE', 'UTF-8'))
def test_process_vars_override(self, patch_getlocale):
mo_file = self.create_mo_file("ga_IE")
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
self.assertEqual(result, mo_file)

def test_find_with_languages(self):
# test that passed languages are used
self.env.set('LANGUAGE', 'pt_BR')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implement a fall back to :func:`locale.getlocale` in :func:`gettext.find` if
*languages* is not provided and :envvar:`LANGUAGES` is not set.
Loading