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::Locale;
use icu::locid::extensions::unicode::{Key, Value};
let loc: Locale = "en-US-u-ca-buddhist".parse()
.expect("Failed to parse.");
assert_eq!(loc.id.language, "en");
assert_eq!(loc.id.script, None);
assert_eq!(loc.id.region, Some("US".parse().unwrap()));
assert_eq!(loc.id.variants.len(), 0);
assert_eq!(loc, "en-US-u-ca-buddhist");
let key: Key = "ca".parse().expect("Parsing key failed.");
let value: Value = "buddhist".parse().expect("Parsing value failed.");
assert_eq!(loc.extensions.unicode.keywords.get(&key),
Some(&value));
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 canonicalization is performed.
Examples
use icu::locid::Locale;
let loc: Locale = "eN_latn_Us-Valencia_u-hC-H12".parse()
.expect("Failed to parse.");
assert_eq!(loc.id.language, "en");
assert_eq!(loc.id.script, Some("Latn".parse().unwrap()));
assert_eq!(loc.id.region, Some("US".parse().unwrap()));
assert_eq!(loc.id.variants.get(0).unwrap(), "valencia");
Fields
id: LanguageIdentifier
extensions: Extensions
Implementations
sourceimpl Locale
impl Locale
sourcepub fn from_bytes(v: &[u8]) -> Result<Self, ParserError>
pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError>
sourcepub const UND: Self = Self {
id: LanguageIdentifier::UND,
extensions: extensions::Extensions::new(),
}
pub const UND: Self = Self { id: LanguageIdentifier::UND, extensions: extensions::Extensions::new(), }
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"), Ok("pl-Latn-PL-u-hc-h12".to_string()));
sourcepub fn cmp_bytes(&self, other: &[u8]) -> Ordering
pub fn cmp_bytes(&self, other: &[u8]) -> Ordering
Compare this Locale
with a BCP-47 string.
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: &[&[u8]] = &[
b"pl-Latn-PL",
b"und",
b"und-fonipa",
b"und-t-m0-true",
b"und-u-ca-hebrew",
b"und-u-ca-japanese",
b"zh",
];
for ab in bcp47_strings.windows(2) {
let a = ab[0];
let b = ab[1];
assert!(a.cmp(b) == Ordering::Less);
let a_langid = Locale::from_bytes(a).unwrap();
assert!(a_langid.cmp_bytes(b) == Ordering::Less);
}
Trait Implementations
sourceimpl AsMut<LanguageIdentifier> for Locale
impl AsMut<LanguageIdentifier> for Locale
sourcefn as_mut(&mut self) -> &mut LanguageIdentifier
fn as_mut(&mut self) -> &mut LanguageIdentifier
Converts this type into a mutable reference of the (usually inferred) input type.
sourceimpl AsRef<LanguageIdentifier> for Locale
impl AsRef<LanguageIdentifier> for Locale
sourcefn as_ref(&self) -> &LanguageIdentifier
fn as_ref(&self) -> &LanguageIdentifier
Converts this type into a shared reference of the (usually inferred) input type.
sourceimpl From<(Language, Option<Script>, Option<Region>)> for Locale
impl From<(Language, Option<Script>, Option<Region>)> for Locale
Examples
use icu::locid::Locale;
use icu::locid::{language, script, region};
let lang = language!("en");
let script = script!("Latn");
let region = region!("US");
let loc = Locale::from((lang, Some(script), Some(region)));
assert_eq!(loc.id.language, "en");
assert_eq!(loc.id.script.unwrap(), "Latn");
assert_eq!(loc.id.region.unwrap(), "US");
assert_eq!(loc.id.variants.len(), 0);
assert_eq!(loc, "en-Latn-US");
sourceimpl From<Language> for Locale
impl From<Language> for Locale
Examples
use icu::locid::Locale;
use icu::locid::language;
let language = language!("en");
let loc = Locale::from(language);
assert_eq!(loc.id.language, "en");
assert_eq!(loc, "en");
sourceimpl From<LanguageIdentifier> for Locale
impl From<LanguageIdentifier> for Locale
sourcefn from(id: LanguageIdentifier) -> Self
fn from(id: LanguageIdentifier) -> Self
Converts to this type from the input type.
sourceimpl From<Locale> for LanguageIdentifier
impl From<Locale> for LanguageIdentifier
sourceimpl From<Option<Region>> for Locale
impl From<Option<Region>> for Locale
Examples
use icu::locid::Locale;
use icu::locid::region;
let region = region!("US");
let loc = Locale::from(Some(region));
assert_eq!(loc.id.region.unwrap(), "US");
assert_eq!(loc, "und-US");
sourceimpl From<Option<Script>> for Locale
impl From<Option<Script>> for Locale
Examples
use icu::locid::Locale;
use icu::locid::script;
let script = script!("latn");
let loc = Locale::from(Some(script));
assert_eq!(loc.id.script.unwrap(), "Latn");
assert_eq!(loc, "und-Latn");
sourceimpl Ord for Locale
impl Ord for Locale
sourceimpl PartialOrd<Locale> for Locale
impl PartialOrd<Locale> for Locale
sourcefn partial_cmp(&self, other: &Locale) -> Option<Ordering>
fn partial_cmp(&self, other: &Locale) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl Writeable for Locale
impl Writeable for Locale
sourcefn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
Writes bytes to the given sink. Errors from the sink are bubbled up.
The default implementation delegates to write_to_parts
, and discards any
Part
annotations. Read more
sourcefn write_len(&self) -> LengthHint
fn write_len(&self) -> LengthHint
Returns a hint for the number of bytes that will be written to the sink. Read more
sourcefn 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,
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. Read more
sourcefn write_to_string(&self) -> Cow<'_, str>
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
impl Eq for Locale
impl StructuralEq for Locale
impl StructuralPartialEq for Locale
Auto Trait Implementations
impl RefUnwindSafe for Locale
impl Send for Locale
impl Sync for Locale
impl Unpin for Locale
impl UnwindSafe for Locale
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more