-
-
Notifications
You must be signed in to change notification settings - Fork 142
feat(linux): use display language for keyboard search #16393
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
Open
mcdurdin
wants to merge
2
commits into
master
Choose a base branch
from
feat/linux/15512-lang-param-for-keyboard-search
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/python3 | ||
| # | ||
| # Keyman is copyright (C) SIL Global. MIT License. | ||
| # | ||
| # Created by Marc Durdin on 2026-08-18 | ||
| # | ||
| # Find the current display locale ID, using the same method as | ||
| # gettext.find() | ||
| # | ||
|
|
||
| import os | ||
| import re | ||
|
|
||
| # | ||
| # @returns array of language identifiers in POSIX style, i.e. 'en_US', not 'en-US'. | ||
| # | ||
| # The returned array always includes the value 'C' for the default static locale, | ||
| # which should be replaced with 'en' when passed to other processes | ||
| # | ||
| def find_locales(): | ||
| # Approach comes from gettext.find() | ||
| languages = [] | ||
| for envvar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): | ||
| val = os.environ.get(envvar) | ||
| if val: | ||
| # technically only LANGUAGE should have multiple entries, | ||
| # but we match gettext's implementation for consistency | ||
| languages = val.split(':') | ||
| break | ||
| if 'C' not in languages: | ||
| languages.append('C') | ||
|
|
||
| # the env vars may also have .encoding or @modifier suffixes that we don't | ||
| # care about | ||
| # https://www.linux.com/news/controlling-your-locale-environment-variables/ | ||
| locale_ids = [] | ||
| for lang in languages: | ||
| locale_id = re.split(r"[.@]", lang) | ||
| locale_ids.append(locale_id[0]) | ||
|
|
||
| return locale_ids | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # Keyman is copyright (C) SIL Global. MIT License. | ||
| # | ||
| # Created by Marc Durdin on 2026-08-19 | ||
| # | ||
| # Unit tests for find_locales | ||
| # | ||
| import os | ||
| from unittest import TestCase, mock | ||
| from keyman_config.keyman_locales import find_locales | ||
|
|
||
| class FindLocalesTests(TestCase): | ||
| @mock.patch.dict(os.environ, {"LANGUAGE": "en_US.UTF-8:de_DE.UTF-8"}, True) | ||
| def test_find_locales_LANGUAGE(self): | ||
| loc = find_locales() | ||
| self.assertEqual(loc, ['en_US', 'de_DE', 'C']) | ||
|
|
||
| @mock.patch.dict(os.environ, {"LANGUAGE": "en_US.UTF-8:de_DE.UTF-8", "LANG": "fr_FR"}, True) | ||
| def test_find_locales_LANGUAGE_and_LANG(self): | ||
| loc = find_locales() | ||
| self.assertEqual(loc, ['en_US', 'de_DE', 'C']) | ||
|
|
||
| @mock.patch.dict(os.environ, {"LANG": "de_DE.UTF-8@euro"}, True) | ||
| def test_find_locales_LANG(self): | ||
| loc = find_locales() | ||
| self.assertEqual(loc, ['de_DE', 'C']) | ||
|
|
||
| @mock.patch.dict(os.environ, {"LC_MESSAGES": "km_Khmr_KH.UTF-8"}, True) | ||
| def test_find_locales_LC_MESSAGES(self): | ||
| loc = find_locales() | ||
| self.assertEqual(loc, ['km_Khmr_KH', 'C']) | ||
|
|
||
| @mock.patch.dict(os.environ, {"LC_ALL": "km_Khmr_KH.UTF-8"}, True) | ||
| def test_find_locales_LC_ALL(self): | ||
| loc = find_locales() | ||
| self.assertEqual(loc, ['km_Khmr_KH', 'C']) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The locale is most often something like
en_US.UTF-8. I'm not sure we want theUTF-8part to be included in the locale.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.
Good catch. Have refreshed and updated according to the best article I could find online about how those variables are used.