Skip to content

Commit

Permalink
x11: logging in Application::get_locale (#1876)
Browse files Browse the repository at this point in the history
thanks @ColinPitrat

#1870 (comment)

Also changed the name and made it a function
  • Loading branch information
maan2003 authored Jul 16, 2021
1 parent 173b560 commit 71faaea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
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

0 comments on commit 71faaea

Please sign in to comment.