Struct icu_locid::LanguageIdentifier
source · [−]pub struct LanguageIdentifier {
pub language: Language,
pub script: Option<Script>,
pub region: Option<Region>,
pub variants: Variants,
}
Expand description
A core struct representing a Unicode BCP47 Language Identifier
.
Examples
use icu::locid::LanguageIdentifier;
let li: LanguageIdentifier = "en-US".parse()
.expect("Failed to parse.");
assert_eq!(li.language, "en");
assert_eq!(li.script, None);
assert_eq!(li.region.unwrap(), "US");
assert_eq!(li.variants.len(), 0);
assert_eq!(li, "en-US");
Parsing
Unicode recognizes three levels of standard conformance for any language identifier:
- well-formed - syntactically correct
- valid - well-formed and only uses registered language, region, script and variant subtags…
- canonical - valid and no deprecated codes or structure.
At the moment parsing normalizes a well-formed language 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 is performed.
Examples
use icu::locid::LanguageIdentifier;
let li: LanguageIdentifier = "eN_latn_Us-Valencia".parse()
.expect("Failed to parse.");
assert_eq!(li.language, "en");
assert_eq!(li.script.unwrap(), "Latn");
assert_eq!(li.region.unwrap(), "US");
assert_eq!(li.variants.get(0).unwrap(), "valencia");
Fields
language: Language
Language subtag of the language identifier.
script: Option<Script>
Script subtag of the language identifier.
region: Option<Region>
Region subtag of the language identifier.
variants: Variants
Variant subtags of the language identifier.
Implementations
sourceimpl LanguageIdentifier
impl LanguageIdentifier
sourcepub fn from_bytes(v: &[u8]) -> Result<Self, ParserError>
pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError>
A constructor which takes a utf8 slice, parses it and
produces a well-formed LanguageIdentifier
.
Examples
use icu::locid::LanguageIdentifier;
let li = LanguageIdentifier::from_bytes(b"en-US")
.expect("Parsing failed.");
assert_eq!(li.to_string(), "en-US");
sourcepub fn from_locale_bytes(v: &[u8]) -> Result<Self, ParserError>
pub fn from_locale_bytes(v: &[u8]) -> Result<Self, ParserError>
A constructor which takes a utf8 slice which may contain extension keys,
parses it and produces a well-formed LanguageIdentifier
.
Examples
use icu::locid::LanguageIdentifier;
let li = LanguageIdentifier::from_locale_bytes(b"en-US-x-posix")
.expect("Parsing failed.");
assert_eq!(li.to_string(), "en-US");
This method should be used for input that may be a locale identifier. All extensions will be lost.
sourcepub const UND: Self = Self {
language: subtags::Language::UND,
script: None,
region: None,
variants: subtags::Variants::new(),
}
pub const UND: Self = Self { language: subtags::Language::UND, script: None, region: None, variants: subtags::Variants::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::LanguageIdentifier;
assert_eq!(LanguageIdentifier::canonicalize("pL_latn_pl"), Ok("pl-Latn-PL".to_string()));
sourcepub fn cmp_bytes(&self, other: &[u8]) -> Ordering
pub fn cmp_bytes(&self, other: &[u8]) -> Ordering
Compare this LanguageIdentifier
with a BCP-47 string.
The return value is equivalent to what would happen if you first converted this
LanguageIdentifier
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::LanguageIdentifier;
use std::cmp::Ordering;
let bcp47_strings: &[&[u8]] = &[
b"pl-Latn-PL",
b"und",
b"und-Adlm",
b"und-GB",
b"und-ZA",
b"und-fonipa",
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 = LanguageIdentifier::from_bytes(a).unwrap();
assert!(a_langid.cmp_bytes(b) == Ordering::Less);
}
Trait Implementations
sourceimpl AsMut<LanguageIdentifier> for LanguageIdentifier
impl AsMut<LanguageIdentifier> for LanguageIdentifier
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 LanguageIdentifier
impl AsRef<LanguageIdentifier> for LanguageIdentifier
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 Clone for LanguageIdentifier
impl Clone for LanguageIdentifier
sourcefn clone(&self) -> LanguageIdentifier
fn clone(&self) -> LanguageIdentifier
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for LanguageIdentifier
impl Debug for LanguageIdentifier
sourceimpl Default for LanguageIdentifier
impl Default for LanguageIdentifier
sourcefn default() -> LanguageIdentifier
fn default() -> LanguageIdentifier
Returns the “default value” for a type. Read more
sourceimpl<'de> Deserialize<'de> for LanguageIdentifier
impl<'de> Deserialize<'de> for LanguageIdentifier
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Display for LanguageIdentifier
impl Display for LanguageIdentifier
sourceimpl From<(Language, Option<Script>, Option<Region>)> for LanguageIdentifier
impl From<(Language, Option<Script>, Option<Region>)> for LanguageIdentifier
Examples
use icu::locid::LanguageIdentifier;
use icu::locid::{language, script, region};
let lang = language!("en");
let script = script!("Latn");
let region = region!("US");
let li = LanguageIdentifier::from((lang, Some(script), Some(region)));
assert_eq!(li.language, "en");
assert_eq!(li.script.unwrap(), "Latn");
assert_eq!(li.region.unwrap(), "US");
assert_eq!(li.variants.len(), 0);
assert_eq!(li, "en-Latn-US");
sourceimpl From<Language> for LanguageIdentifier
impl From<Language> for LanguageIdentifier
Examples
use icu::locid::LanguageIdentifier;
use icu::locid::language;
let language = language!("en");
let li = LanguageIdentifier::from(language);
assert_eq!(li.language, "en");
assert_eq!(li, "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 LanguageIdentifier
impl From<Option<Region>> for LanguageIdentifier
Examples
use icu::locid::LanguageIdentifier;
use icu::locid::region;
let region = region!("US");
let li = LanguageIdentifier::from(Some(region));
assert_eq!(li.region.unwrap(), "US");
assert_eq!(li, "und-US");
sourceimpl From<Option<Script>> for LanguageIdentifier
impl From<Option<Script>> for LanguageIdentifier
Examples
use icu::locid::LanguageIdentifier;
use icu::locid::script;
let script = script!("latn");
let li = LanguageIdentifier::from(Some(script));
assert_eq!(li.script.unwrap(), "Latn");
assert_eq!(li, "und-Latn");
sourceimpl FromStr for LanguageIdentifier
impl FromStr for LanguageIdentifier
sourceimpl Hash for LanguageIdentifier
impl Hash for LanguageIdentifier
sourceimpl Ord for LanguageIdentifier
impl Ord for LanguageIdentifier
sourceimpl PartialEq<&'_ str> for LanguageIdentifier
impl PartialEq<&'_ str> for LanguageIdentifier
sourceimpl PartialEq<LanguageIdentifier> for LanguageIdentifier
impl PartialEq<LanguageIdentifier> for LanguageIdentifier
sourcefn eq(&self, other: &LanguageIdentifier) -> bool
fn eq(&self, other: &LanguageIdentifier) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &LanguageIdentifier) -> bool
fn ne(&self, other: &LanguageIdentifier) -> bool
This method tests for !=
.
sourceimpl PartialEq<str> for LanguageIdentifier
impl PartialEq<str> for LanguageIdentifier
sourceimpl PartialOrd<LanguageIdentifier> for LanguageIdentifier
impl PartialOrd<LanguageIdentifier> for LanguageIdentifier
sourcefn partial_cmp(&self, other: &LanguageIdentifier) -> Option<Ordering>
fn partial_cmp(&self, other: &LanguageIdentifier) -> 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 Serialize for LanguageIdentifier
impl Serialize for LanguageIdentifier
sourceimpl Writeable for LanguageIdentifier
impl Writeable for LanguageIdentifier
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 LanguageIdentifier
impl StructuralEq for LanguageIdentifier
impl StructuralPartialEq for LanguageIdentifier
Auto Trait Implementations
impl RefUnwindSafe for LanguageIdentifier
impl Send for LanguageIdentifier
impl Sync for LanguageIdentifier
impl Unpin for LanguageIdentifier
impl UnwindSafe for LanguageIdentifier
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