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.
§Examples
use icu::locid::{
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"))
);
§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 bogus subtags will cause the parsing to fail with an error.
No subtag validation or alias resolution is performed.
§Examples
use icu::locid::{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
impl Locale
Sourcepub fn try_from_bytes(v: &[u8]) -> Result<Self, ParserError>
pub fn try_from_bytes(v: &[u8]) -> Result<Self, ParserError>
Sourcepub fn canonicalize<S: AsRef<[u8]>>(input: S) -> Result<String, ParserError>
pub fn canonicalize<S: AsRef<[u8]>>(input: S) -> Result<String, ParserError>
This is a best-effort operation that performs all available levels of canonicalization.
At the moment the operation will normalize casing and the separator, but in the future it may also validate and update from deprecated subtags to canonical ones.
§Examples
use icu::locid::Locale;
assert_eq!(
Locale::canonicalize("pL_latn_pl-U-HC-H12").as_deref(),
Ok("pl-Latn-PL-u-hc-h12")
);
Sourcepub fn strict_cmp(&self, other: &[u8]) -> Ordering
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::locid::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);
}
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 strict_cmp_iter<'l, I>(&self, subtags: I) -> SubtagOrderingResult<I>
👎Deprecated since 1.5.0: if you need this, please file an issue
pub fn strict_cmp_iter<'l, I>(&self, subtags: I) -> SubtagOrderingResult<I>
Compare this Locale
with an iterator of BCP-47 subtags.
This function has the same equality semantics as Locale::strict_cmp
. It is intended as
a more modular version that allows multiple subtag iterators to be chained together.
For an additional example, see SubtagOrderingResult
.
§Examples
use icu::locid::locale;
use std::cmp::Ordering;
let subtags: &[&[u8]] =
&[b"ca", b"ES", b"valencia", b"u", b"ca", b"hebrew"];
let loc = locale!("ca-ES-valencia-u-ca-hebrew");
assert_eq!(
Ordering::Equal,
loc.strict_cmp_iter(subtags.iter().copied()).end()
);
let loc = locale!("ca-ES-valencia");
assert_eq!(
Ordering::Less,
loc.strict_cmp_iter(subtags.iter().copied()).end()
);
let loc = locale!("ca-ES-valencia-u-nu-arab");
assert_eq!(
Ordering::Greater,
loc.strict_cmp_iter(subtags.iter().copied()).end()
);
Sourcepub fn normalizing_eq(&self, other: &str) -> bool
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::locid::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 AsMut<LanguageIdentifier> for Locale
impl AsMut<LanguageIdentifier> for Locale
Source§fn as_mut(&mut self) -> &mut LanguageIdentifier
fn as_mut(&mut self) -> &mut LanguageIdentifier
Source§impl AsRef<LanguageIdentifier> for Locale
impl AsRef<LanguageIdentifier> for Locale
Source§fn as_ref(&self) -> &LanguageIdentifier
fn as_ref(&self) -> &LanguageIdentifier
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.
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§impl From<(Language, Option<Script>, Option<Region>)> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{
locale,
subtags::{language, region, script},
};
assert_eq!(
Locale::from((
language!("en"),
Some(script!("Latn")),
Some(region!("US"))
)),
locale!("en-Latn-US")
);
impl From<(Language, Option<Script>, Option<Region>)> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{
locale,
subtags::{language, region, script},
};
assert_eq!(
Locale::from((
language!("en"),
Some(script!("Latn")),
Some(region!("US"))
)),
locale!("en-Latn-US")
);
Source§impl From<Language> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{locale, subtags::language};
assert_eq!(Locale::from(language!("en")), locale!("en"));
impl From<Language> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{locale, subtags::language};
assert_eq!(Locale::from(language!("en")), locale!("en"));
Source§impl From<LanguageIdentifier> for Locale
impl From<LanguageIdentifier> for Locale
Source§fn from(id: LanguageIdentifier) -> Self
fn from(id: LanguageIdentifier) -> Self
Source§impl From<Locale> for LanguageIdentifier
impl From<Locale> for LanguageIdentifier
Source§impl From<Option<Region>> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{locale, subtags::region};
assert_eq!(Locale::from(Some(region!("US"))), locale!("und-US"));
impl From<Option<Region>> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{locale, subtags::region};
assert_eq!(Locale::from(Some(region!("US"))), locale!("und-US"));
Source§impl From<Option<Script>> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{locale, subtags::script};
assert_eq!(Locale::from(Some(script!("latn"))), locale!("und-Latn"));
impl From<Option<Script>> for Locale
§Examples
use icu::locid::Locale;
use icu::locid::{locale, subtags::script};
assert_eq!(Locale::from(Some(script!("latn"))), locale!("und-Latn"));
Source§impl Writeable for Locale
impl Writeable for Locale
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.