-
-
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?
Conversation
request: @tomasr8 |
@serhiy-storchaka Could you please review this? |
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.
There are issues with using locale.getlocale()
. It ignores the modifier and can raise ValueError. I suggest to use locale._setlocale()
.
Needed tests for locales "C", "C.UTF-8", locales with a modifier, locales with and without encoding.
To help testing, you can extract the code into the _guess_languages()
function.
There are several existing tests (from a previous pr) for the C locale. |
@@ -131,7 +131,7 @@ install themselves in the built-in namespace as the function :func:`!_`. | |||
|
|||
If *localedir* is not given, then the default system locale directory is used. | |||
[#]_ If *languages* is not given, then the environment variable :envvar:`LANGUAGE` | |||
is searched, it falls back to :func:`locale.getlocale`, which in turn falls | |||
is searched, it falls back to :func:`locale.setlocale`, which in turn falls |
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 ..."?
self.env.unset(var) | ||
|
||
@unittest.mock.patch("locale.getlocale", return_value=('ga_IE', 'UTF-8')) | ||
def test_process_vars_override(self, patch_getlocale): | ||
@unittest.mock.patch("locale.setlocale", return_value=(None, None)) |
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.
Note that LANGUAGE takes priority over the current locale. This is not tested.
result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) | ||
self.assertEqual(result, mo_file) | ||
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 comment
The 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, "ga_IE") | ||
self._for_all_vars(mo_file, "ga_IE.UTF-8") | ||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
locale.setlocale()
returns a string, not a tuple. This is why you need also tests with a real, not mocked locale.setlocale()
.
Test also with modifiers, "C" and "C.UTF-8".
Test that locale overrides 'LC_ALL', 'LC_MESSAGES', 'LANG', but 'LANGUAGE' overrides locale.
LANGUAGES envvar is prioritized over getlocale for consistency with C gettext.
📚 Documentation preview 📚: https://cpython-previews--131477.org.readthedocs.build/