//! Global locale instances and system inspection. //! //! This is an auxiliary crate for i18n solutions that: //! //! - Holds the appropriate default instances of locale. //! - Inspects the system for the initial values. //! //! You don't want to use it directly, but instead use an internationalisation crate like [locale]. //! //! This crate is separate and intentionally minimal so that multiple i18n crates or multiple //! versions of one that get into the application still share the current locale setting. //! //! [locale]: https://crates.io/crates/locale #[macro_use] extern crate lazy_static; extern crate regex; #[cfg(target_os = "macos")] #[macro_use] extern crate objc; use regex::Regex; use std::borrow::{Borrow,Cow}; use std::cell::RefCell; use std::convert::AsRef; use std::fmt; use std::sync::Mutex; // ------------------------------ LANGUAGE RANGE --------------------------------- /// Language and culture identifier. /// /// This object holds a [RFC4647] extended language range. /// /// The internal data may be owned or shared from object with lifetime `'a`. The lifetime can be /// extended using the `into_static()` method, which internally clones the data as needed. /// /// # Syntax /// /// The range is composed of `-`-separated alphanumeric subtags, possibly replaced by `*`s. It /// might be empty. /// /// In agreement with [RFC4647], this object only requires that the tag matches: /// /// ```ebnf /// language_tag = (alpha{1,8} | "*") /// ("-" (alphanum{1,8} | "*"))* /// ``` /// /// The exact interpretation is up to the downstream localization provider, but it expected that /// it will be matched against a normalized [RFC5646] language tag, which has the structure: /// /// ```ebnf /// language_tag = language /// ("-" script)? /// ("-" region)? /// ("-" variant)* /// ("-" extension)* /// ("-" private)? /// /// language = alpha{2,3} ("-" alpha{3}){0,3} /// /// script = aplha{4} /// /// region = alpha{2} /// | digit{3} /// /// variant = alphanum{5,8} /// | digit alphanum{3} /// /// extension = [0-9a-wyz] ("-" alphanum{2,8})+ /// /// private = "x" ("-" alphanum{1,8})+ /// ``` /// /// * `language` is an [ISO639] 2-letter or, where not defined, 3-letter code. A code for /// macro-language might be followed by code of specific dialect. /// * `script` is an [ISO15924] 4-letter code. /// * `region` is either an [ISO3166] 2-letter code or, for areas other than countries, [UN M.49] /// 3-digit numeric code. /// * `variant` is a string indicating variant of the language. /// * `extension` and `private` define additional options. The private part has same structure as /// the Unicode [`-u-` extension][u_ext]. Available options are documented for the facets that /// use them. /// /// The values obtained by inspecting the system are normalized according to those rules. /// /// The content will be case-normalized as recommended in [RFC5646] §2.1.1, namely: /// /// * `language` is written in lowercase, /// * `script` is written with first capital, /// * `country` is written in uppercase and /// * all other subtags are written in lowercase. /// /// When detecting system configuration, additional options that may be generated under the /// [`-u-` extension][u_ext] currently are: /// /// * `cf` — Currency format (`account` for parenthesized negative values, `standard` for minus /// sign). /// * `fw` — First day of week (`mon` to `sun`). /// * `hc` — Hour cycle (`h12` for 1–12, `h23` for 0–23). /// * `ms` — Measurement system (`metric` or `ussystem`). /// * `nu` — Numbering system—only decimal systems are currently used. /// * `va` — Variant when locale is specified in Unix format and the tag after `@` does not /// correspond to any variant defined in [Language subtag registry]. /// /// And under the `-x-` extension, following options are defined: /// /// * `df` — Date format: /// /// * `iso`: Short date should be in ISO format of `yyyy-MM-dd`. /// /// For example `-df-iso`. /// /// * `dm` — Decimal separator for monetary: /// /// Followed by one or more Unicode codepoints in hexadecimal. For example `-dm-002d` means to /// use comma. /// /// * `ds` — Decimal separator for numbers: /// /// Followed by one or more Unicode codepoints in hexadecimal. For example `-ds-002d` means to /// use comma. /// /// * `gm` — Group (thousand) separator for monetary: /// /// Followed by one or more Unicode codepoints in hexadecimal. For example `-dm-00a0` means to /// use non-breaking space. /// /// * `gs` — Group (thousand) separator for numbers: /// /// Followed by one or more Unicode codepoints in hexadecimal. For example `-ds-00a0` means to /// use non-breaking space. /// /// * `ls` — List separator: /// /// Followed by one or more Unicode codepoints in hexadecimal. For example, `-ds-003b` means to /// use a semicolon. /// /// [RFC5646]: https://www.rfc-editor.org/rfc/rfc5646.txt /// [RFC4647]: https://www.rfc-editor.org/rfc/rfc4647.txt /// [ISO639]: https://en.wikipedia.org/wiki/ISO_639 /// [ISO15924]: https://en.wikipedia.org/wiki/ISO_15924 /// [ISO3166]: https://en.wikipedia.org/wiki/ISO_3166 /// [UN M.49]: https://en.wikipedia.org/wiki/UN_M.49 /// [u_ext]: http://www.unicode.org/reports/tr35/#u_Extension /// [Language subtag registry]: https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry #[derive(Clone,Debug,Eq,Hash,PartialEq)] pub struct LanguageRange<'a> { language: Cow<'a, str> } lazy_static! { static ref REGULAR_LANGUAGE_RANGE_REGEX: Regex = Regex::new(r"(?x) ^ (?P (?: [[:alpha:]]{2,3} (?: - [[:alpha:]]{3} ){0,3} | \* )) (?P