pub struct DataLocale(/* private fields */);
Expand description

An adapter between DataLocale and ecma402_traits::Locale.

Methods from Deref<Target = DataLocale>§

source

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

Compare this DataLocale with BCP-47 bytes.

The return value is equivalent to what would happen if you first converted this DataLocale 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_locid::Locale;
use icu_provider::DataLocale;
use std::cmp::Ordering;

let bcp47_strings: &[&str] = &[
    "ca",
    "ca+EUR",
    "ca-ES",
    "ca-ES+GBP",
    "ca-ES+GBP+short",
    "ca-ES+USD",
    "ca-ES-u-ca-buddhist",
    "ca-ES-valencia",
    "cat",
    "pl-Latn-PL",
    "und",
    "und+MXN",
    "und-fonipa",
    "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_eq!(a.cmp(b), Ordering::Less, "strings: {} < {}", a, b);
    let a_loc: DataLocale = a.parse().unwrap();
    assert_eq!(
        a_loc.strict_cmp(a.as_bytes()),
        Ordering::Equal,
        "strict_cmp: {} == {}",
        a_loc,
        a
    );
    assert_eq!(
        a_loc.strict_cmp(b.as_bytes()),
        Ordering::Less,
        "strict_cmp: {} < {}",
        a_loc,
        b
    );
    let b_loc: DataLocale = b.parse().unwrap();
    assert_eq!(
        b_loc.strict_cmp(b.as_bytes()),
        Ordering::Equal,
        "strict_cmp: {} == {}",
        b_loc,
        b
    );
    assert_eq!(
        b_loc.strict_cmp(a.as_bytes()),
        Ordering::Greater,
        "strict_cmp: {} > {}",
        b_loc,
        a
    );
}

Comparison against invalid strings:

use icu_provider::DataLocale;

let invalid_strings: &[&str] = &[
    // Less than "ca-ES"
    "CA",
    "ar+GBP+FOO",
    // Greater than "ca-ES+GBP"
    "ca_ES",
    "ca-ES+GBP+FOO",
];

let data_locale = "ca-ES+GBP".parse::<DataLocale>().unwrap();

for s in invalid_strings.iter() {
    let expected_ordering = "ca-ES+GBP".cmp(s);
    let actual_ordering = data_locale.strict_cmp(s.as_bytes());
    assert_eq!(expected_ordering, actual_ordering, "{}", s);
}
source

pub fn is_empty(&self) -> bool

Returns whether this DataLocale has all empty fields (no components).

See also:

Examples
use icu_provider::DataLocale;

assert!("und".parse::<DataLocale>().unwrap().is_empty());
assert!(!"und-u-ca-buddhist"
    .parse::<DataLocale>()
    .unwrap()
    .is_empty());
assert!(!"und+auxiliary".parse::<DataLocale>().unwrap().is_empty());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_empty());
source

pub fn is_und(&self) -> bool

Returns whether this DataLocale is und in the locale and extensions portion.

This ignores auxiliary keys.

See also:

Examples
use icu_provider::DataLocale;

assert!("und".parse::<DataLocale>().unwrap().is_und());
assert!(!"und-u-ca-buddhist".parse::<DataLocale>().unwrap().is_und());
assert!("und+auxiliary".parse::<DataLocale>().unwrap().is_und());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_und());
source

pub fn is_langid_und(&self) -> bool

Returns whether the [LanguageIdentifier] associated with this request is und.

This ignores extension keywords and auxiliary keys.

See also:

Examples
use icu_provider::DataLocale;

assert!("und".parse::<DataLocale>().unwrap().is_langid_und());
assert!("und-u-ca-buddhist"
    .parse::<DataLocale>()
    .unwrap()
    .is_langid_und());
assert!("und+auxiliary"
    .parse::<DataLocale>()
    .unwrap()
    .is_langid_und());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_langid_und());
source

pub fn get_langid(&self) -> LanguageIdentifier

Gets the [LanguageIdentifier] for this DataLocale.

This may allocate memory if there are variant subtags. If you need only the language, script, and/or region subtag, use the specific getters for those subtags:

If you have ownership over the DataLocale, use DataLocale::into_locale() and then access the id field.

Examples
use icu_locid::langid;
use icu_provider::prelude::*;

const FOO_BAR: DataKey = icu_provider::data_key!("foo/bar@1");

let req_no_langid = DataRequest {
    locale: &Default::default(),
    metadata: Default::default(),
};

let req_with_langid = DataRequest {
    locale: &langid!("ar-EG").into(),
    metadata: Default::default(),
};

assert_eq!(req_no_langid.locale.get_langid(), langid!("und"));
assert_eq!(req_with_langid.locale.get_langid(), langid!("ar-EG"));
source

pub fn language(&self) -> Language

Returns the [Language] for this DataLocale.

source

pub fn script(&self) -> Option<Script>

Returns the [Script] for this DataLocale.

source

pub fn region(&self) -> Option<Region>

Returns the [Region] for this DataLocale.

source

pub fn has_variants(&self) -> bool

Returns whether there are any [Variant] subtags in this DataLocale.

source

pub fn get_unicode_ext(&self, key: &Key) -> Option<Value>

Gets the value of the specified Unicode extension keyword for this DataLocale.

source

pub fn has_unicode_ext(&self) -> bool

Returns whether there are any Unicode extension keywords in this DataLocale.

source

pub fn contains_unicode_ext(&self, key: &Key) -> bool

Returns whether a specific Unicode extension keyword is present in this DataLocale.

source

pub fn matches_unicode_ext(&self, key: &Key, value: &Value) -> bool

Returns whether this DataLocale contains a Unicode extension keyword with the specified key and value.

Examples
use icu_locid::{
    extensions::unicode::{key, value},
    Locale,
};
use icu_provider::prelude::*;

let locale: Locale = "it-IT-u-ca-coptic".parse().expect("Valid BCP-47");
let locale: DataLocale = locale.into();

assert_eq!(locale.get_unicode_ext(&key!("hc")), None);
assert_eq!(locale.get_unicode_ext(&key!("ca")), Some(value!("coptic")));
assert!(locale.matches_unicode_ext(&key!("ca"), &value!("coptic"),));

Trait Implementations§

source§

impl Clone for DataLocale

source§

fn clone(&self) -> DataLocale

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 DataLocale

source§

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

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

impl Deref for DataLocale

§

type Target = DataLocale

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Display for DataLocale

source§

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

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

impl Hash for DataLocale

source§

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

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 DataLocale

source§

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

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

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

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

impl Locale for DataLocale

source§

impl StructuralPartialEq for DataLocale

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

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

source§

impl<T> MaybeSendSync for T