fix: get_locale follows posix standard (#1264)

* fix: get_locale follows posix standard

* refactor: more concise and legible get_locale function

Co-authored-by: Wölfchen <w-lfchen@posteo.net>

---------

Co-authored-by: Wölfchen <w-lfchen@posteo.net>
This commit is contained in:
Mirko Hahn 2025-01-04 21:20:26 +01:00 committed by GitHub
parent 6ee166707f
commit a7bd80ac1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 8 deletions

View file

@ -16,6 +16,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Fix values in the `EWW_NET` variable (By: mario-kr) - Fix values in the `EWW_NET` variable (By: mario-kr)
- Fix the gtk `expander` widget (By: ovalkonia) - Fix the gtk `expander` widget (By: ovalkonia)
- Fix wayland monitor names support (By: dragonnn) - Fix wayland monitor names support (By: dragonnn)
- `get_locale` now follows POSIX standard for locale selection (By: mirhahn, w-lfchen)
### Features ### Features
- Add OnDemand support for focusable on wayland (By: GallowsDove) - Add OnDemand support for focusable on wayland (By: GallowsDove)

View file

@ -1,14 +1,12 @@
use chrono::Locale; use chrono::Locale;
use std::env::var; use std::env::var;
/// Returns the `Locale` enum based on the `LC_TIME` environment variable. /// Returns the `Locale` enum based on the `LC_ALL`, `LC_TIME`, and `LANG` environment variables in
/// that order, which is the precedence order prescribed by Section 8.2 of POSIX.1-2017.
/// If the environment variable is not defined or is malformed use the POSIX locale. /// If the environment variable is not defined or is malformed use the POSIX locale.
pub fn get_locale() -> Locale { pub fn get_locale() -> Locale {
let locale_string: String = var("LC_ALL")
var("LC_TIME").map_or_else(|_| "C".to_string(), |v| v.split(".").next().unwrap_or("C").to_string()); .or_else(|_| var("LC_TIME"))
.or_else(|_| var("LANG"))
match (&*locale_string).try_into() { .map_or(Locale::POSIX, |v| v.split('.').next().and_then(|x| x.try_into().ok()).unwrap_or_default())
Ok(x) => x,
Err(_) => Locale::POSIX,
}
} }