Skip to content
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

x11: logging in Application::get_locale #1876

Merged
merged 6 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ You can find its changes [documented below](#070---2021-01-01).
- Updated to x11rb 0.8.0. ([#1519] by [@psychon])
- Updated fluent-bundle to 0.15.1 and fluent syntax to 0.11.0 ([#1772] by [@r-ml])
- Updated usvg to 0.14.1 ([#1802] by [@r-ml])
- x11: Add logging to `Application::get_locale` ([#1876] by [@Maan2003])

### Outside News

Expand Down Expand Up @@ -761,6 +762,7 @@ Last release without a changelog :(
[#1867]: https://github.com/linebender/druid/pull/1867
[#1868]: https://github.com/linebender/druid/pull/1868
[#1873]: https://github.com/linebender/druid/pull/1873
[#1876]: https://github.com/linebender/druid/pull/1876

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
33 changes: 24 additions & 9 deletions druid-shell/src/backend/x11/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,21 +770,36 @@ impl Application {
}

pub fn get_locale() -> String {
let var_non_empty = |var| match std::env::var(var) {
Ok(s) if s.is_empty() => None,
Ok(s) => Some(s),
Err(_) => None,
};
fn locale_env_var(var: &str) -> Option<String> {
match std::env::var(var) {
Ok(s) if s.is_empty() => {
tracing::debug!("locale: ignoring empty env var {}", var);
None
}
Ok(s) => {
tracing::debug!("locale: env var {} found: {:?}", var, &s);
Some(s)
}
Err(std::env::VarError::NotPresent) => {
tracing::debug!("locale: env var {} not found", var);
None
}
Err(std::env::VarError::NotUnicode(_)) => {
tracing::debug!("locale: ignoring invalid unicode env var {}", var);
None
}
}
}

// from gettext manual
// https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html#Locale-Environment-Variables
let mut locale = var_non_empty("LANGUAGE")
let mut locale = locale_env_var("LANGUAGE")
// the LANGUAGE value is priority list seperated by :
// See: https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html#The-LANGUAGE-variable
.and_then(|locale| locale.split(':').next().map(String::from))
.or_else(|| var_non_empty("LC_ALL"))
.or_else(|| var_non_empty("LC_MESSAGES"))
.or_else(|| var_non_empty("LANG"))
.or_else(|| locale_env_var("LC_ALL"))
.or_else(|| locale_env_var("LC_MESSAGES"))
.or_else(|| locale_env_var("LANG"))
.unwrap_or_else(|| "en-US".to_string());

// This is done because the locale parsing library we use expects an unicode locale, but these vars have an ISO locale
Expand Down