Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-64243: Implementlocale.getlocale fall back ingettext.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
StanFromIreland wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
fromStanFromIreland:improve-find

Conversation

StanFromIreland
Copy link
Contributor

@StanFromIrelandStanFromIreland commentedMar 19, 2025
edited by github-actionsbot
Loading

LANGUAGES envvar is prioritized over getlocale for consistency with C gettext.


📚 Documentation preview 📚:https://cpython-previews--131477.org.readthedocs.build/

@StanFromIreland
Copy link
ContributorAuthor

request:@tomasr8

@StanFromIreland
Copy link
ContributorAuthor

@serhiy-storchaka Could you please review this?

@serhiy-storchakaserhiy-storchaka self-requested a reviewApril 30, 2025 18:57
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a 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 usinglocale.getlocale(). It ignores the modifier and can raise ValueError. I suggest to uselocale._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.

@StanFromIreland
Copy link
ContributorAuthor

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Mentioninglocale.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))

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")

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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Test also with a non-euro modifier (@euro is handled specially), e.g. "ca_ES@valencia" or "ks_IN@devanagari". Test that the modifier is not ignored. For@euro, test also with encoding.

Test also "C" and "C.UTF-8".


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')):

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 mockedlocale.setlocale().

Test also with modifiers, "C" and "C.UTF-8".

Test that locale overrides 'LC_ALL', 'LC_MESSAGES', 'LANG', but 'LANGUAGE' overrides locale.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@tomasr8tomasr8Awaiting requested review from tomasr8

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@StanFromIreland@serhiy-storchaka@tomasr8

[8]ページ先頭

©2009-2025 Movatter.jp