Skip to content

Commit c85d0c2

Browse files
Serhiy's review
1 parent 4f37342 commit c85d0c2

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

Doc/library/gettext.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ install themselves in the built-in namespace as the function :func:`!_`.
131131

132132
If *localedir* is not given, then the default system locale directory is used.
133133
[#]_ If *languages* is not given, then the environment variable :envvar:`LANGUAGE`
134-
is searched, it falls back to :func:`locale.getlocale`, which in turn falls
134+
is searched, it falls back to :func:`locale.setlocale`, which in turn falls
135135
back to the environment variables :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
136136
:envvar:`LANG` where the first one returning a non-empty value is used for the
137137
*languages* variable. The environment variables should contain a colon separated
@@ -149,7 +149,7 @@ install themselves in the built-in namespace as the function :func:`!_`.
149149
the environment variables.
150150

151151
.. versionchanged:: next
152-
:func:`locale.getlocale` is used to generate *languages* if *languages* is
152+
:func:`locale.setlocale` is used to generate *languages* if *languages* is
153153
not provided.
154154

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

Lib/gettext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ def func(n):
230230

231231

232232
def _expand_lang(loc):
233-
import locale
234233
loc = locale.normalize(loc)
235234
COMPONENT_CODESET = 1 << 0
236235
COMPONENT_TERRITORY = 1 << 1
@@ -494,7 +493,7 @@ def find(domain, localedir=None, languages=None, all=False):
494493
languages = []
495494
if val := os.environ.get('LANGUAGE'):
496495
languages = val.split(':')
497-
elif (loc := locale.getlocale()) != (None, None):
496+
elif (loc := locale.setlocale(locale.LC_MESSAGES)) != (None, None):
498497
languages = [".".join(filter(None, loc))]
499498
else:
500499
for envar in ('LC_ALL', 'LC_MESSAGES', 'LANG'):

Lib/test/test_gettext.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import locale
21
import os
32
import base64
43
import gettext
@@ -737,30 +736,32 @@ def create_mo_file(self, lang):
737736
f.write(GNU_MO_DATA)
738737
return mo_file
739738

740-
@unittest.mock.patch("locale.getlocale", return_value=(None, None))
741-
def test_find_with_env_vars(self, patch_getlocale):
742-
# test that find correctly finds the environment variables
743-
# when languages are not supplied
744-
mo_file = self.create_mo_file("ga_IE")
745-
for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
746-
self.env.set(var, 'ga_IE')
747-
result = gettext.find("mofile",
748-
localedir=os.path.join(self.tempdir, "locale"))
749-
self.assertEqual(result, mo_file)
750-
self.env.unset(var)
751-
# test fallbacks
739+
def _for_all_vars(self, mo_file, locale):
752740
for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
753-
self.env.set(var, 'es_ES:ga_IE:fr_FR')
741+
self.env.set(var, locale)
754742
result = gettext.find("mofile",
755743
localedir=os.path.join(self.tempdir, "locale"))
756-
self.assertEqual(result, mo_file)
744+
self.assertEqual(mo_file, result)
757745
self.env.unset(var)
758746

759-
@unittest.mock.patch("locale.getlocale", return_value=('ga_IE', 'UTF-8'))
760-
def test_process_vars_override(self, patch_getlocale):
747+
@unittest.mock.patch("locale.setlocale", return_value=(None, None))
748+
def test_find_with_env_vars(self, patch_getlocale):
749+
# test that find correctly finds the environment variables
750+
# when languages are not supplied
761751
mo_file = self.create_mo_file("ga_IE")
762-
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
763-
self.assertEqual(result, mo_file)
752+
self._for_all_vars(mo_file, "ga_IE")
753+
self._for_all_vars(mo_file, "ga_IE.UTF-8")
754+
self._for_all_vars(mo_file, "es_ES:ga_IE:fr_FR")
755+
self._for_all_vars(mo_file, "ga_IE@euro")
756+
757+
def test_process_vars_override(self):
758+
mo_file = self.create_mo_file("ga_IE")
759+
with unittest.mock.patch("locale.setlocale", return_value=('ga_IE', 'UTF-8')):
760+
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
761+
self.assertEqual(mo_file, result)
762+
with unittest.mock.patch("locale.setlocale", return_value=('ga_IE', None)):
763+
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
764+
self.assertEqual(mo_file, result)
764765

765766
def test_find_with_languages(self):
766767
# test that passed languages are used

0 commit comments

Comments
 (0)