Struct icu::locale::Locale

source ·
pub struct Locale {
    pub id: LanguageIdentifier,
    pub extensions: Extensions,
}
Expand description

A core struct representing a Unicode Locale Identifier.

A locale is made of two parts:

  • Unicode Language Identifier
  • A set of Unicode Extensions

Locale exposes all of the same fields and methods as LanguageIdentifier, and on top of that is able to parse, manipulate and serialize unicode extension fields.

§Parsing

Unicode recognizes three levels of standard conformance for a locale:

  • well-formed - syntactically correct
  • valid - well-formed and only uses registered language subtags, extensions, keywords, types…
  • canonical - valid and no deprecated codes or structure.

At the moment parsing normalizes a well-formed locale identifier converting _ separators to - and adjusting casing to conform to the Unicode standard.

Any syntactically invalid subtags will cause the parsing to fail with an error.

This operation normalizes syntax to be well-formed. No legacy subtag replacements is performed. For validation and canonicalization, see LocaleCanonicalizer.

§Ordering

This type deliberately does not implement Ord or PartialOrd because there are multiple possible orderings, and the team did not want to favor one over any other.

Instead, there are functions available that return these different orderings:

See issue: https://github.com/unicode-org/icu4x/issues/1215

§Examples

Simple example:

use icu::locale::{
    extensions::unicode::{key, value},
    locale,
    subtags::{language, region},
};

let loc = locale!("en-US-u-ca-buddhist");

assert_eq!(loc.id.language, language!("en"));
assert_eq!(loc.id.script, None);
assert_eq!(loc.id.region, Some(region!("US")));
assert_eq!(loc.id.variants.len(), 0);
assert_eq!(
    loc.extensions.unicode.keywords.get(&key!("ca")),
    Some(&value!("buddhist"))
);

More complex example:

use icu::locale::{subtags::*, Locale};

let loc: Locale = "eN_latn_Us-Valencia_u-hC-H12"
    .parse()
    .expect("Failed to parse.");

assert_eq!(loc.id.language, "en".parse::<Language>().unwrap());
assert_eq!(loc.id.script, "Latn".parse::<Script>().ok());
assert_eq!(loc.id.region, "US".parse::<Region>().ok());
assert_eq!(
    loc.id.variants.get(0),
    "valencia".parse::<Variant>().ok().as_ref()
);

Fields§

§id: LanguageIdentifier

The basic language/script/region components in the locale identifier along with any variants.

§extensions: Extensions

Any extensions present in the locale identifier.

Implementations§

source§

impl Locale

source

pub fn try_from_str(s: &str) -> Result<Locale, ParseError>

A constructor which takes a utf8 slice, parses it and produces a well-formed Locale.

§Examples
use icu::locale::Locale;

Locale::try_from_str("en-US-u-hc-h12").unwrap();
source

pub fn try_from_utf8(code_units: &[u8]) -> Result<Locale, ParseError>

source

pub const fn default() -> Locale

Const-friendly version of Default::default.

source

pub fn normalize_utf8(input: &[u8]) -> Result<Cow<'_, str>, ParseError>

Normalize the locale (operating on UTF-8 formatted byte slices)

This operation will normalize casing and the separator.

§Examples
use icu::locale::Locale;

assert_eq!(
    Locale::normalize_utf8(b"pL_latn_pl-U-HC-H12").as_deref(),
    Ok("pl-Latn-PL-u-hc-h12")
);
source

pub fn normalize(input: &str) -> Result<Cow<'_, str>, ParseError>

Normalize the locale (operating on strings)

This operation will normalize casing and the separator.

§Examples
use icu::locale::Locale;

assert_eq!(
    Locale::normalize("pL_latn_pl-U-HC-H12").as_deref(),
    Ok("pl-Latn-PL-u-hc-h12")
);
source

pub fn strict_cmp(&self, other: &[u8]) -> Ordering

Compare this Locale with BCP-47 bytes.

The return value is equivalent to what would happen if you first converted this Locale to a BCP-47 string and then performed a byte comparison.

This function is case-sensitive and results in a total order, so it is appropriate for binary search. The only argument producing Ordering::Equal is self.to_string().

§Examples
use icu::locale::Locale;
use std::cmp::Ordering;

let bcp47_strings: &[&str] = &[
    "pl-Latn-PL",
    "und",
    "und-fonipa",
    "und-t-m0-true",
    "und-u-ca-hebrew",
    "und-u-ca-japanese",
    "zh",
];

for ab in bcp47_strings.windows(2) {
    let a = ab[0];
    let b = ab[1];
    assert!(a.cmp(b) == Ordering::Less);
    let a_loc = a.parse::<Locale>().unwrap();
    assert!(a_loc.strict_cmp(a.as_bytes()) == Ordering::Equal);
    assert!(a_loc.strict_cmp(b.as_bytes()) == Ordering::Less);
}
source

pub fn total_cmp(&self, other: &Locale) -> Ordering

Returns an ordering suitable for use in BTreeSet.

Unlike Locale::strict_cmp, the ordering may or may not be equivalent to string ordering, and it may or may not be stable across ICU4X releases.

§Examples

Using a wrapper to add one of these to a BTreeSet:

use icu::locale::Locale;
use std::cmp::Ordering;
use std::collections::BTreeSet;

#[derive(PartialEq, Eq)]
struct LocaleTotalOrd(Locale);

impl Ord for LocaleTotalOrd {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.total_cmp(&other.0)
    }
}

impl PartialOrd for LocaleTotalOrd {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

let _: BTreeSet<LocaleTotalOrd> = unimplemented!();
source

pub fn normalizing_eq(&self, other: &str) -> bool

Compare this Locale with a potentially unnormalized BCP-47 string.

The return value is equivalent to what would happen if you first parsed the BCP-47 string to a Locale and then performed a structural comparison.

§Examples
use icu::locale::Locale;

let bcp47_strings: &[&str] = &[
    "pl-LaTn-pL",
    "uNd",
    "UND-FONIPA",
    "UnD-t-m0-TrUe",
    "uNd-u-CA-Japanese",
    "ZH",
];

for a in bcp47_strings {
    assert!(a.parse::<Locale>().unwrap().normalizing_eq(a));
}

Trait Implementations§

source§

impl Clone for Locale

source§

fn clone(&self) -> Locale

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Locale

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Locale

source§

fn default() -> Locale

Returns the “default value” for a type. Read more
source§

impl Display for Locale

This trait is implemented for compatibility with fmt!. To create a string, [Writeable::write_to_string] is usually more efficient.

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl From<&Locale> for CollatorPreferences

source§

fn from(loc: &Locale) -> CollatorPreferences

Converts to this type from the input type.
source§

impl From<&Locale> for ListFormatterPreferences

source§

fn from(loc: &Locale) -> ListFormatterPreferences

Converts to this type from the input type.
source§

impl From<&Locale> for LocalePreferences

source§

fn from(loc: &Locale) -> LocalePreferences

Converts to this type from the input type.
source§

impl From<&Locale> for PluralRulesPreferences

source§

fn from(loc: &Locale) -> PluralRulesPreferences

Converts to this type from the input type.
source§

impl From<(Language, Option<Script>, Option<Region>)> for Locale

§Examples

use icu::locale::Locale;
use icu::locale::{
    locale,
    subtags::{language, region, script},
};

assert_eq!(
    Locale::from((
        language!("en"),
        Some(script!("Latn")),
        Some(region!("US"))
    )),
    locale!("en-Latn-US")
);
source§

fn from(lsr: (Language, Option<Script>, Option<Region>)) -> Locale

Converts to this type from the input type.
source§

impl From<CollatorPreferences> for Locale

source§

fn from(other: CollatorPreferences) -> Locale

Converts to this type from the input type.
source§

impl From<Language> for Locale

§Examples

use icu::locale::Locale;
use icu::locale::{locale, subtags::language};

assert_eq!(Locale::from(language!("en")), locale!("en"));
source§

fn from(language: Language) -> Locale

Converts to this type from the input type.
source§

impl From<LanguageIdentifier> for Locale

source§

fn from(id: LanguageIdentifier) -> Locale

Converts to this type from the input type.
source§

impl From<ListFormatterPreferences> for Locale

source§

fn from(other: ListFormatterPreferences) -> Locale

Converts to this type from the input type.
source§

impl From<Locale> for CollatorPreferences

source§

fn from(loc: Locale) -> CollatorPreferences

Converts to this type from the input type.
source§

impl From<Locale> for LanguageIdentifier

source§

fn from(loc: Locale) -> LanguageIdentifier

Converts to this type from the input type.
source§

impl From<Locale> for ListFormatterPreferences

source§

fn from(loc: Locale) -> ListFormatterPreferences

Converts to this type from the input type.
source§

impl From<Locale> for PluralRulesPreferences

source§

fn from(loc: Locale) -> PluralRulesPreferences

Converts to this type from the input type.
source§

impl From<LocalePreferences> for Locale

source§

fn from(prefs: LocalePreferences) -> Locale

Converts to this type from the input type.
source§

impl From<Option<Region>> for Locale

§Examples

use icu::locale::Locale;
use icu::locale::{locale, subtags::region};

assert_eq!(Locale::from(Some(region!("US"))), locale!("und-US"));
source§

fn from(region: Option<Region>) -> Locale

Converts to this type from the input type.
source§

impl From<Option<Script>> for Locale

§Examples

use icu::locale::Locale;
use icu::locale::{locale, subtags::script};

assert_eq!(Locale::from(Some(script!("latn"))), locale!("und-Latn"));
source§

fn from(script: Option<Script>) -> Locale

Converts to this type from the input type.
source§

impl From<PluralRulesPreferences> for Locale

source§

fn from(other: PluralRulesPreferences) -> Locale

Converts to this type from the input type.
source§

impl FromStr for Locale

source§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Locale, <Locale as FromStr>::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Locale

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Locale

source§

fn eq(&self, other: &Locale) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Writeable for Locale

source§

fn write_to<W>(&self, sink: &mut W) -> Result<(), Error>
where W: Write + ?Sized,

Writes a string to the given sink. Errors from the sink are bubbled up. The default implementation delegates to write_to_parts, and discards any Part annotations.
source§

fn writeable_length_hint(&self) -> LengthHint

Returns a hint for the number of UTF-8 bytes that will be written to the sink. Read more
source§

fn write_to_string(&self) -> Cow<'_, str>

Creates a new String with the data from this Writeable. Like ToString, but smaller and faster. Read more
source§

fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error>
where S: PartsWrite + ?Sized,

Write bytes and Part annotations to the given sink. Errors from the sink are bubbled up. The default implementation delegates to write_to, and doesn’t produce any Part annotations.
source§

impl Eq for Locale

source§

impl StructuralPartialEq for Locale

Auto Trait Implementations§

§

impl Freeze for Locale

§

impl RefUnwindSafe for Locale

§

impl Send for Locale

§

impl Sync for Locale

§

impl Unpin for Locale

§

impl UnwindSafe for Locale

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T
where T: Send + Sync,