-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -736,17 +736,33 @@ def create_mo_file(self, lang): | |
f.write(GNU_MO_DATA) | ||
return mo_file | ||
|
||
def test_find_with_env_vars(self): | ||
# test that find correctly finds the environment variables | ||
# when languages are not supplied | ||
mo_file = self.create_mo_file("ga_IE") | ||
def _for_all_vars(self, mo_file, locale): | ||
for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): | ||
self.env.set(var, 'ga_IE') | ||
self.env.set(var, locale) | ||
result = gettext.find("mofile", | ||
localedir=os.path.join(self.tempdir, "locale")) | ||
self.assertEqual(result, mo_file) | ||
self.assertEqual(mo_file, result) | ||
self.env.unset(var) | ||
|
||
@unittest.mock.patch("locale.setlocale", return_value=(None, None)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that LANGUAGE takes priority over the current locale. This is not tested. |
||
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") | ||
self._for_all_vars(mo_file, "ga_IE") | ||
self._for_all_vars(mo_file, "ga_IE.UTF-8") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test that the encoding is not ignored, and "ga_IE.UTF-8.mo" is found. |
||
self._for_all_vars(mo_file, "es_ES:ga_IE:fr_FR") | ||
self._for_all_vars(mo_file, "ga_IE@euro") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
def test_process_vars_override(self): | ||
mo_file = self.create_mo_file("ga_IE") | ||
with unittest.mock.patch("locale.setlocale", return_value=('ga_IE', 'UTF-8')): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Test also with modifiers, "C" and "C.UTF-8". Test that locale overrides 'LC_ALL', 'LC_MESSAGES', 'LANG', but 'LANGUAGE' overrides locale. |
||
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) | ||
self.assertEqual(mo_file, result) | ||
with unittest.mock.patch("locale.setlocale", return_value=('ga_IE', None)): | ||
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) | ||
self.assertEqual(mo_file, result) | ||
|
||
def test_find_with_languages(self): | ||
# test that passed languages are used | ||
self.env.set('LANGUAGE', 'pt_BR') | ||
|
@@ -798,7 +814,7 @@ def test__all__(self): | |
unittest.main() | ||
|
||
|
||
# For reference, here's the .po file used to created the GNU_MO_DATA above. | ||
# For reference, here's the .po file used to create the GNU_MO_DATA above. | ||
# | ||
# The original version was automatically generated from the sources with | ||
# pygettext. Later it was manually modified to add plural forms support. | ||
|
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:`LANGUAGE` is not set. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mentioning
locale.setlocale()
here looks weird, as we do not change the current locale. It is just an implementation detail. Maybe just say "the current locale"?"... it falls back to the current locale or to the environment variables ..."?