Skip to content

Commit

Permalink
1.6.0-pre.1 and Backport v2 -> v1: Adjusts langs() to match POSIX loc…
Browse files Browse the repository at this point in the history
…ale spec (#151)

Co-authored-by: Chase Johnson <cjohnson19@pm.me>
  • Loading branch information
AldaronLau and cjohnson19 authored Feb 26, 2025
1 parent a027097 commit d90a4f8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whoami"
version = "1.6.0-pre.0"
version = "1.6.0-pre.1"
edition = "2018"
license = "Apache-2.0 OR BSL-1.0 OR MIT"
documentation = "https://docs.rs/whoami"
Expand Down
2 changes: 1 addition & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn langs() -> Result<impl Iterator<Item = Language>> {
// FIXME: Could do less allocation
let langs = Target::langs(Os)?;
let langs = langs
.split(';')
.split(':')
.map(ToString::to_string)
.filter_map(|lang| {
let lang = lang
Expand Down
3 changes: 2 additions & 1 deletion src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ impl Display for Country {
/// language code followed an forward slash and uppercase country code (example:
/// `en/US`).
///
/// Uses <https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes>
/// Language codes defined in ISO 639 <https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes>,
/// Country codes defined in ISO 3166 <https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2>
#[non_exhaustive]
#[derive(Clone, Eq, PartialEq, Debug)]
// #[allow(variant_size_differences)]
Expand Down
16 changes: 12 additions & 4 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,18 @@ fn unix_lang() -> Result<String> {
Error::new(kind, e)
})
};
let langs = check_var("LC_ALL")
.or_else(|_| check_var("LANGS"))
.or_else(|_| check_var("LANG"))
.or_else(|_| check_var("LANGUAGE"))?;

// Uses priority defined in
// <https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html>
let locale = check_var("LC_ALL").or_else(|_| check_var("LANG"));

// The LANGUAGE environment variable takes precedence if and only if
// localization is enabled, i.e., LC_ALL / LANG is not "C".
// <https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html>
let langs = match &locale {
Ok(loc) if loc != "C" => check_var("LANGUAGE").or(locale),
_ => locale,
}?;

if langs.is_empty() {
return Err(err_empty_record());
Expand Down

0 comments on commit d90a4f8

Please sign in to comment.