1Password / sys-locale

A small and lightweight Rust library to obtain the active locale on the system.
Apache License 2.0
75 stars 15 forks source link

Returns "en_US" on Linux. Should return "en-US". #3

Closed John-Nagle closed 1 year ago

John-Nagle commented 2 years ago

sys-locale is supposed to return a valid BCP-47 language tag. Calling this on a US-based Ubuntu 20.04 LTS system returns "en_US". Note the underscore, not a dash.

Documentation for pub fn get_locale() -> Option<String> says it "returns Some(String) with a BCP-47 language tag inside."

The spec for BCP-47 is in RFC 5646, section 2.1, "Syntax".

"A language tag is composed from a sequence of one or more "subtags", each of which refines or narrows the range of language identified by the overall tag. Subtags, in turn, are a sequence of alphanumeric characters (letters and digits), distinguished and separated from other subtags in a tag by a hyphen ("-", [Unicode] U+002D)."

So RFC5646, BCP-47 requires a hyphen, not an underscore.

This breaks something that uses the tag. OxiLangTag, a Rust library allowing to validate and normalize language tags following RFC 5646 (BCP 47), doesn't like the underscore:

Err value: LanguageTagParseError { kind: InvalidLanguage }'

so there is software that fails because of this.

The Unix/Linux LANG environment variable isn't BCP 47 compatible. It's close, but not the same. So, the Unix case in sys-locale needs to do some reformatting. Thanks.

complexspaces commented 1 year ago

Thanks again for reporting this, I apologize it took so long to be resolved. This fix is now published in 0.2.3

John-Nagle commented 1 year ago

Thanks. It's a small problem, but did break something.