Skip to content

Commit fcce7bd

Browse files
Initial
1 parent b8367e7 commit fcce7bd

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Doc/library/gettext.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ install themselves in the built-in namespace as the function :func:`!_`.
130130
strings, where each string is a language code.
131131

132132
If *localedir* is not given, then the default system locale directory is used.
133-
[#]_ If *languages* is not given, then the following environment variables are
134-
searched: :envvar:`LANGUAGE`, :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
135-
:envvar:`LANG`. The first one returning a non-empty value is used for the
133+
[#]_ 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
135+
back to the environment variables :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
136+
:envvar:`LANG` where the first one returning a non-empty value is used for the
136137
*languages* variable. The environment variables should contain a colon separated
137138
list of languages, which will be split on the colon to produce the expected list
138139
of language code strings.
@@ -147,6 +148,9 @@ install themselves in the built-in namespace as the function :func:`!_`.
147148
of all file names, in the order in which they appear in the languages list or
148149
the environment variables.
149150

151+
.. versionchanged:: next
152+
:func:`locale.getlocale` is used to generate *languages* if *languages* is
153+
not provided.
150154

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

Lib/gettext.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import operator
4747
import os
4848
import sys
49+
import locale
4950

5051

5152
__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
@@ -491,11 +492,16 @@ def find(domain, localedir=None, languages=None, all=False):
491492
localedir = _default_localedir
492493
if languages is None:
493494
languages = []
494-
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
495-
val = os.environ.get(envar)
496-
if val:
497-
languages = val.split(':')
498-
break
495+
if os.environ.get('LANGUAGE'):
496+
languages = os.environ.get('LANGUAGE').split(',')
497+
elif locale.getlocale() != (None, None):
498+
languages.append(".".join(filter(None, locale.getlocale())))
499+
else:
500+
for envar in ('LC_ALL', 'LC_MESSAGES', 'LANG'):
501+
val = os.environ.get(envar)
502+
if val:
503+
languages = val.split(':')
504+
break
499505
if 'C' not in languages:
500506
languages.append('C')
501507
# now normalize and expand the languages

Lib/test/test_gettext.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import locale
12
import os
23
import base64
34
import gettext
@@ -736,7 +737,8 @@ def create_mo_file(self, lang):
736737
f.write(GNU_MO_DATA)
737738
return mo_file
738739

739-
def test_find_with_env_vars(self):
740+
@unittest.mock.patch("locale.getlocale", return_value=(None, None))
741+
def test_find_with_env_vars(self, patch_getlocale):
740742
# test that find correctly finds the environment variables
741743
# when languages are not supplied
742744
mo_file = self.create_mo_file("ga_IE")
@@ -747,6 +749,12 @@ def test_find_with_env_vars(self):
747749
self.assertEqual(result, mo_file)
748750
self.env.unset(var)
749751

752+
@unittest.mock.patch("locale.getlocale", return_value=('ga_IE', 'UTF-8'))
753+
def test_process_vars_override(self, patch_getlocale):
754+
mo_file = self.create_mo_file("ga_IE")
755+
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"))
756+
self.assertEqual(result, mo_file)
757+
750758
def test_find_with_languages(self):
751759
# test that passed languages are used
752760
self.env.set('LANGUAGE', 'pt_BR')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Implement a fall back to :func:`locale.getlocale` in :func:`gettext.find` if
2+
*languages* is not provided and :envvar:`LANGUAGES` is not set.

0 commit comments

Comments
 (0)