pub struct DataLocale { /* private fields */ }
Expand description

The main locale type used by the ICU4X data provider.

DataLocale contains less functionality than Locale but more than LanguageIdentifier for better size and performance while still meeting the needs of the ICU4X data pipeline.

Examples

Convert a Locale to a DataLocale and back:

use icu_locid::locale;
use icu_provider::DataLocale;

let locale = locale!("en-u-ca-buddhist");
let data_locale = DataLocale::from(locale);
let locale = data_locale.into_locale();

assert_eq!(locale, locale!("en-u-ca-buddhist"));

You can alternatively create a DataLocale from a borrowed Locale, which is more efficient than cloning the Locale, but less efficient than converting an owned Locale:

use icu_locid::locale;
use icu_provider::DataLocale;

let locale1 = locale!("en-u-ca-buddhist");
let data_locale = DataLocale::from(&locale1);
let locale2 = data_locale.into_locale();

assert_eq!(locale1, locale2);

If you are sure that you have no Unicode keywords, start with LanguageIdentifier:

use icu_locid::langid;
use icu_provider::DataLocale;

let langid = langid!("es-CA-valencia");
let data_locale = DataLocale::from(langid);
let langid = data_locale.get_langid();

assert_eq!(langid, langid!("es-CA-valencia"));

DataLocale only supports -u keywords, to reflect the current state of CLDR data lookup and fallback. This may change in the future.

use icu_locid::{locale, Locale};
use icu_provider::DataLocale;

let locale = "hi-t-en-h0-hybrid-u-attr-ca-buddhist"
    .parse::<Locale>()
    .unwrap();
let data_locale = DataLocale::from(locale);

assert_eq!(data_locale.into_locale(), locale!("hi-u-ca-buddhist"));

Implementations§

source§

impl 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-ES",
    "ca-ES-u-ca-buddhist",
    "ca-ES-valencia",
    "pl-Latn-PL",
    "und",
    "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!(a.cmp(b) == Ordering::Less);
    let a_loc: DataLocale = a.parse::<Locale>().unwrap().into();
    assert!(
        a_loc.strict_cmp(a.as_bytes()) == Ordering::Equal,
        "{} == {}",
        a,
        a
    );
    assert!(
        a_loc.strict_cmp(b.as_bytes()) == Ordering::Less,
        "{} < {}",
        a,
        b
    );
    let b_loc: DataLocale = b.parse::<Locale>().unwrap().into();
    assert!(
        b_loc.strict_cmp(b.as_bytes()) == Ordering::Equal,
        "{} == {}",
        b,
        b
    );
    assert!(
        b_loc.strict_cmp(a.as_bytes()) == Ordering::Greater,
        "{} > {}",
        b,
        a
    );
}
source§

impl DataLocale

source

pub fn is_empty(&self) -> bool

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

source

pub fn is_langid_und(&self) -> bool

Returns whether the LanguageIdentifier associated with this request is und.

Note that this only checks the language identifier; extension keywords may also be set. To check the entire DataLocale, use DataLocale::is_empty().

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 set_langid(&mut self, lid: LanguageIdentifier)

Overrides the entire LanguageIdentifier portion of this DataLocale.

source

pub fn into_locale(self) -> Locale

Converts this DataLocale into a Locale.

See also DataLocale::get_langid().

Examples
use icu_locid::{
    langid, locale, subtags_language as language, subtags_region as region,
    Locale,
};
use icu_provider::prelude::*;

let locale: DataLocale = locale!("it-IT-u-ca-coptic").into();

assert_eq!(locale.get_langid(), langid!("it-IT"));
assert_eq!(locale.language(), language!("it"));
assert_eq!(locale.script(), None);
assert_eq!(locale.region(), Some(region!("IT")));

let locale = locale.into_locale();
assert_eq!(locale, locale!("it-IT-u-ca-coptic"));
source

pub fn language(&self) -> Language

Returns the Language for this DataLocale.

source

pub fn set_language(&mut self, language: Language)

Returns the Language for this DataLocale.

source

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

Returns the Script for this DataLocale.

source

pub fn set_script(&mut self, script: Option<Script>)

Sets the Script for this DataLocale.

source

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

Returns the Region for this DataLocale.

source

pub fn set_region(&mut self, region: Option<Region>)

Sets 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 set_variants(&mut self, variants: Variants)

Sets all Variants on this DataLocale, overwriting any that were there previously.

source

pub fn clear_variants(&mut self) -> Variants

Removes all 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 as key, extensions_unicode_value as 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"),));
source

pub fn set_unicode_ext(&mut self, key: Key, value: Value) -> Option<Value>

Sets the value for a specific Unicode extension keyword on this DataLocale.

source

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

Removes a specific Unicode extension keyword from this DataLocale, returning the value if it was present.

source

pub fn retain_unicode_ext<F>(&mut self, predicate: F)where F: FnMut(&Key) -> bool,

Retains a subset of keywords as specified by the predicate function.

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<'a> Default for &'a DataLocale

source§

fn default() -> Self

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

impl Default for DataLocale

source§

fn default() -> DataLocale

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

impl Display for DataLocale

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

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

impl From<&LanguageIdentifier> for DataLocale

source§

fn from(langid: &LanguageIdentifier) -> Self

Converts to this type from the input type.
source§

impl From<&Locale> for DataLocale

source§

fn from(locale: &Locale) -> Self

Converts to this type from the input type.
source§

impl From<LanguageIdentifier> for DataLocale

source§

fn from(langid: LanguageIdentifier) -> Self

Converts to this type from the input type.
source§

impl From<Locale> for DataLocale

source§

fn from(locale: Locale) -> Self

Converts to this type from the input type.
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<DataLocale> 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 Writeable for DataLocale

source§

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

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 DataLocale

source§

impl StructuralEq 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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.
source§

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

source§

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