Struct icu_provider::DataLocale
source · pub struct DataLocale {
langid: LanguageIdentifier,
keywords: Keywords,
aux: Option<AuxiliaryKeys>,
}
Expand description
A locale type optimized for use in fallbacking and the ICU4X data pipeline.
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"));
Fields§
§langid: LanguageIdentifier
§keywords: Keywords
§aux: Option<AuxiliaryKeys>
Implementations§
source§impl DataLocale
impl DataLocale
sourcepub fn strict_cmp(&self, other: &[u8]) -> Ordering
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_provider::DataLocale;
use std::cmp::Ordering;
let bcp47_strings: &[&str] = &[
"ca",
"ca-ES",
"ca-ES-u-ca-buddhist",
"ca-ES-valencia",
"ca-ES-x-gbp",
"ca-ES-x-gbp-short",
"ca-ES-x-usd",
"ca-ES-xyzabc",
"ca-x-eur",
"cat",
"pl-Latn-PL",
"und",
"und-fonipa",
"und-u-ca-hebrew",
"und-u-ca-japanese",
"und-x-mxn",
"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-x-gbp-FOO",
// Greater than "ca-ES-x-gbp"
"ca_ES",
"ca-ES-x-gbp-FOO",
];
let data_locale = "ca-ES-x-gbp".parse::<DataLocale>().unwrap();
for s in invalid_strings.iter() {
let expected_ordering = "ca-ES-x-gbp".cmp(s);
let actual_ordering = data_locale.strict_cmp(s.as_bytes());
assert_eq!(expected_ordering, actual_ordering, "{}", s);
}
source§impl DataLocale
impl DataLocale
sourcepub fn is_empty(&self) -> bool
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-x-aux".parse::<DataLocale>().unwrap().is_empty());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_empty());
sourcepub fn total_cmp(&self, other: &Self) -> Ordering
pub fn total_cmp(&self, other: &Self) -> Ordering
Returns an ordering suitable for use in BTreeSet
.
The ordering may or may not be equivalent to string ordering, and it may or may not be stable across ICU4X releases.
sourcepub fn is_und(&self) -> bool
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-x-aux".parse::<DataLocale>().unwrap().is_und());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_und());
sourcepub fn is_langid_und(&self) -> bool
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-x-aux".parse::<DataLocale>().unwrap().is_langid_und());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_langid_und());
sourcepub fn get_langid(&self) -> LanguageIdentifier
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"));
sourcepub fn set_langid(&mut self, lid: LanguageIdentifier)
pub fn set_langid(&mut self, lid: LanguageIdentifier)
Overrides the entire LanguageIdentifier
portion of this DataLocale
.
sourcepub fn into_locale(self) -> Locale
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, region},
};
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"));
Auxiliary keys are retained:
use icu_provider::prelude::*;
use writeable::assert_writeable_eq;
let data_locale: DataLocale = "und-u-nu-arab-x-gbp".parse().unwrap();
assert_writeable_eq!(data_locale, "und-u-nu-arab-x-gbp");
let recovered_locale = data_locale.into_locale();
assert_writeable_eq!(recovered_locale, "und-u-nu-arab-x-gbp");
sourcepub fn language(&self) -> Language
pub fn language(&self) -> Language
Returns the Language
for this DataLocale
.
sourcepub fn set_language(&mut self, language: Language)
pub fn set_language(&mut self, language: Language)
Returns the Language
for this DataLocale
.
sourcepub fn set_script(&mut self, script: Option<Script>)
pub fn set_script(&mut self, script: Option<Script>)
Sets the Script
for this DataLocale
.
sourcepub fn set_region(&mut self, region: Option<Region>)
pub fn set_region(&mut self, region: Option<Region>)
Sets the Region
for this DataLocale
.
sourcepub fn has_variants(&self) -> bool
pub fn has_variants(&self) -> bool
Returns whether there are any Variant
subtags in this DataLocale
.
sourcepub fn set_variants(&mut self, variants: Variants)
pub fn set_variants(&mut self, variants: Variants)
Sets all Variants
on this DataLocale
, overwriting any that were there previously.
sourcepub fn clear_variants(&mut self) -> Variants
pub fn clear_variants(&mut self) -> Variants
Removes all Variant
subtags in this DataLocale
.
sourcepub fn get_unicode_ext(&self, key: &Key) -> Option<Value>
pub fn get_unicode_ext(&self, key: &Key) -> Option<Value>
Gets the value of the specified Unicode extension keyword for this DataLocale
.
sourcepub fn has_unicode_ext(&self) -> bool
pub fn has_unicode_ext(&self) -> bool
Returns whether there are any Unicode extension keywords in this DataLocale
.
sourcepub fn contains_unicode_ext(&self, key: &Key) -> bool
pub fn contains_unicode_ext(&self, key: &Key) -> bool
Returns whether a specific Unicode extension keyword is present in this DataLocale
.
sourcepub fn matches_unicode_ext(&self, key: &Key, value: &Value) -> bool
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};
use icu_provider::prelude::*;
let locale: DataLocale = "it-IT-u-ca-coptic".parse().expect("Valid BCP-47");
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"),));
sourcepub fn set_unicode_ext(&mut self, key: Key, value: Value) -> Option<Value>
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
.
sourcepub fn remove_unicode_ext(&mut self, key: &Key) -> Option<Value>
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.
sourcepub fn retain_unicode_ext<F>(&mut self, predicate: F)
pub fn retain_unicode_ext<F>(&mut self, predicate: F)
Retains a subset of keywords as specified by the predicate function.
sourcepub fn get_aux(&self) -> Option<&AuxiliaryKeys>
pub fn get_aux(&self) -> Option<&AuxiliaryKeys>
Gets the auxiliary key for this DataLocale
.
For more information and examples, see AuxiliaryKeys
.
sourcepub fn has_aux(&self) -> bool
pub fn has_aux(&self) -> bool
Returns whether this DataLocale
has an auxiliary key.
For more information and examples, see AuxiliaryKeys
.
sourcepub fn set_aux(&mut self, value: AuxiliaryKeys) -> Option<AuxiliaryKeys>
pub fn set_aux(&mut self, value: AuxiliaryKeys) -> Option<AuxiliaryKeys>
Sets an auxiliary key on this DataLocale
.
Returns the previous auxiliary key if present.
For more information and examples, see AuxiliaryKeys
.
sourcepub fn remove_aux(&mut self) -> Option<AuxiliaryKeys>
pub fn remove_aux(&mut self) -> Option<AuxiliaryKeys>
Remove an auxiliary key, if present. Returns the removed auxiliary key.
§Examples
use icu_locid::langid;
use icu_provider::prelude::*;
use writeable::assert_writeable_eq;
let mut data_locale: DataLocale = langid!("ar-EG").into();
let aux = "gbp"
.parse::<AuxiliaryKeys>()
.expect("contains valid characters");
data_locale.set_aux(aux);
assert_writeable_eq!(data_locale, "ar-EG-x-gbp");
let maybe_aux = data_locale.remove_aux();
assert_writeable_eq!(data_locale, "ar-EG");
assert_writeable_eq!(maybe_aux.unwrap(), "gbp");
Trait Implementations§
source§impl Clone for DataLocale
impl Clone for DataLocale
source§fn clone(&self) -> DataLocale
fn clone(&self) -> DataLocale
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for DataLocale
impl Debug for DataLocale
source§impl<'a> Default for &'a DataLocale
impl<'a> Default for &'a DataLocale
source§impl Default for DataLocale
impl Default for DataLocale
source§fn default() -> DataLocale
fn default() -> DataLocale
source§impl Display for DataLocale
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§impl From<&LanguageIdentifier> for DataLocale
impl From<&LanguageIdentifier> for DataLocale
source§fn from(langid: &LanguageIdentifier) -> Self
fn from(langid: &LanguageIdentifier) -> Self
source§impl From<&Locale> for DataLocale
impl From<&Locale> for DataLocale
source§impl From<LanguageIdentifier> for DataLocale
impl From<LanguageIdentifier> for DataLocale
source§fn from(langid: LanguageIdentifier) -> Self
fn from(langid: LanguageIdentifier) -> Self
source§impl From<Locale> for DataLocale
impl From<Locale> for DataLocale
source§impl FromStr for DataLocale
impl FromStr for DataLocale
source§impl Hash for DataLocale
impl Hash for DataLocale
source§impl PartialEq for DataLocale
impl PartialEq for DataLocale
source§fn eq(&self, other: &DataLocale) -> bool
fn eq(&self, other: &DataLocale) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl Writeable for DataLocale
impl Writeable for DataLocale
source§fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
write_to_parts
, and discards any
Part
annotations.source§fn writeable_length_hint(&self) -> LengthHint
fn writeable_length_hint(&self) -> LengthHint
source§fn write_to_string(&self) -> Cow<'_, str>
fn write_to_string(&self) -> Cow<'_, str>
String
with the data from this Writeable
. Like ToString
,
but smaller and faster. Read moresource§fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error>where
S: PartsWrite + ?Sized,
fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error>where
S: PartsWrite + ?Sized,
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.