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::{
    langid,
    subtags::{language, region},
};

let li = langid!("en-US");

assert_eq!(li.language, language!("en"));
assert_eq!(li.script, None);
assert_eq!(li.region, Some(region!("US")));
assert_eq!(li.variants.len(), 0);

§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::{
    langid,
    subtags::{language, region, script, variant},
};

let li = langid!("eN_latn_Us-Valencia");

assert_eq!(li.language, language!("en"));
assert_eq!(li.script, Some(script!("Latn")));
assert_eq!(li.region, Some(region!("US")));
assert_eq!(li.variants.get(0), Some(&variant!("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§

source§

impl LanguageIdentifier

source

pub fn try_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;

LanguageIdentifier::try_from_bytes(b"en-US").expect("Parsing failed");
source

pub fn try_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::{langid, LanguageIdentifier};

let li = LanguageIdentifier::try_from_locale_bytes(b"en-US-x-posix")
    .expect("Parsing failed.");

assert_eq!(li, langid!("en-US"));

This method should be used for input that may be a locale identifier. All extensions will be lost.

source

pub const UND: Self = _

The default undefined language “und”. Same as default().

§Examples
use icu::locid::LanguageIdentifier;

assert_eq!(LanguageIdentifier::default(), LanguageIdentifier::UND);
source

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").as_deref(),
    Ok("pl-Latn-PL")
);
source

pub fn strict_cmp(&self, other: &[u8]) -> Ordering

Compare this LanguageIdentifier with BCP-47 bytes.

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: &[&str] = &[
    "pl-Latn-PL",
    "und",
    "und-Adlm",
    "und-GB",
    "und-ZA",
    "und-fonipa",
    "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 = a.parse::<LanguageIdentifier>().unwrap();
    assert!(a_langid.strict_cmp(a.as_bytes()) == Ordering::Equal);
    assert!(a_langid.strict_cmp(b.as_bytes()) == Ordering::Less);
}
source

pub(crate) fn as_tuple( &self, ) -> (Language, Option<Script>, Option<Region>, &Variants)

source

pub fn total_cmp(&self, other: &Self) -> Ordering

Compare this LanguageIdentifier with another LanguageIdentifier field-by-field. The result is a total ordering sufficient for use in a BTreeMap.

Unlike Self::strict_cmp, this function’s ordering may not equal string ordering.

source

pub fn strict_cmp_iter<'l, I>(&self, subtags: I) -> SubtagOrderingResult<I>
where I: Iterator<Item = &'l [u8]>,

👎Deprecated since 1.5.0: if you need this, please file an issue

Compare this LanguageIdentifier with an iterator of BCP-47 subtags.

This function has the same equality semantics as LanguageIdentifier::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::LanguageIdentifier;
use std::cmp::Ordering;

let subtags: &[&[u8]] = &[b"ca", b"ES", b"valencia"];

let loc = "ca-ES-valencia".parse::<LanguageIdentifier>().unwrap();
assert_eq!(
    Ordering::Equal,
    loc.strict_cmp_iter(subtags.iter().copied()).end()
);

let loc = "ca-ES".parse::<LanguageIdentifier>().unwrap();
assert_eq!(
    Ordering::Less,
    loc.strict_cmp_iter(subtags.iter().copied()).end()
);

let loc = "ca-ZA".parse::<LanguageIdentifier>().unwrap();
assert_eq!(
    Ordering::Greater,
    loc.strict_cmp_iter(subtags.iter().copied()).end()
);
source

pub fn normalizing_eq(&self, other: &str) -> bool

Compare this LanguageIdentifier 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 LanguageIdentifier and then performed a structural comparison.

§Examples
use icu::locid::LanguageIdentifier;

let bcp47_strings: &[&str] = &[
    "pl-LaTn-pL",
    "uNd",
    "UnD-adlm",
    "uNd-GB",
    "UND-FONIPA",
    "ZH",
];

for a in bcp47_strings {
    assert!(a.parse::<LanguageIdentifier>().unwrap().normalizing_eq(a));
}
source

pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F) -> Result<(), E>
where F: FnMut(&str) -> Result<(), E>,

source

pub(crate) fn for_each_subtag_str_lowercased<E, F>( &self, f: &mut F, ) -> Result<(), E>
where F: FnMut(&str) -> Result<(), E>,

Executes f on each subtag string of this LanguageIdentifier, with every string in lowercase ascii form.

The default canonicalization of language identifiers uses titlecase scripts and uppercase regions. However, this differs from RFC6497 (BCP 47 Extension T), which specifies:

The canonical form for all subtags in the extension is lowercase, with the fields ordered by the separators, alphabetically.

Hence, this method is used inside Transform Extensions to be able to get the correct canonicalization of the language identifier.

As an example, the canonical form of locale EN-LATN-CA-T-EN-LATN-CA is en-Latn-CA-t-en-latn-ca, with the script and region parts lowercased inside T extensions, but titlecased and uppercased outside T extensions respectively.

source

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

Writes this LanguageIdentifier to a sink, replacing uppercase ascii chars with lowercase ascii chars.

The default canonicalization of language identifiers uses titlecase scripts and uppercase regions. However, this differs from RFC6497 (BCP 47 Extension T), which specifies:

The canonical form for all subtags in the extension is lowercase, with the fields ordered by the separators, alphabetically.

Hence, this method is used inside Transform Extensions to be able to get the correct canonicalization of the language identifier.

As an example, the canonical form of locale EN-LATN-CA-T-EN-LATN-CA is en-Latn-CA-t-en-latn-ca, with the script and region parts lowercased inside T extensions, but titlecased and uppercased outside T extensions respectively.

Trait Implementations§

source§

impl AsMut<LanguageIdentifier> for LanguageIdentifier

source§

fn as_mut(&mut self) -> &mut Self

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsMut<LanguageIdentifier> for Locale

source§

fn as_mut(&mut self) -> &mut LanguageIdentifier

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsRef<LanguageIdentifier> for LanguageIdentifier

source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<LanguageIdentifier> for Locale

source§

fn as_ref(&self) -> &LanguageIdentifier

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for LanguageIdentifier

source§

fn clone(&self) -> LanguageIdentifier

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 LanguageIdentifier

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl Default for LanguageIdentifier

source§

fn default() -> LanguageIdentifier

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

impl Display for LanguageIdentifier

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 (Language, Option<Script>, Option<Region>)

Convert from a LanguageIdentifier to an LSR tuple.

§Examples

use icu::locid::{
    langid,
    subtags::{language, region, script},
};

let lid = langid!("en-Latn-US");
let (lang, script, region) = (&lid).into();

assert_eq!(lang, language!("en"));
assert_eq!(script, Some(script!("Latn")));
assert_eq!(region, Some(region!("US")));
source§

fn from(langid: &LanguageIdentifier) -> Self

Converts to this type from the input type.
source§

impl From<(Language, Option<Script>, Option<Region>)> for LanguageIdentifier

Convert from an LSR tuple to a LanguageIdentifier.

§Examples

use icu::locid::{
    langid,
    subtags::{language, region, script},
    LanguageIdentifier,
};

let lang = language!("en");
let script = script!("Latn");
let region = region!("US");
assert_eq!(
    LanguageIdentifier::from((lang, Some(script), Some(region))),
    langid!("en-Latn-US")
);
source§

fn from(lsr: (Language, Option<Script>, Option<Region>)) -> Self

Converts to this type from the input type.
source§

impl From<Language> for LanguageIdentifier

§Examples

use icu::locid::{langid, subtags::language, LanguageIdentifier};

assert_eq!(LanguageIdentifier::from(language!("en")), langid!("en"));
source§

fn from(language: Language) -> Self

Converts to this type from the input type.
source§

impl From<LanguageIdentifier> for Locale

source§

fn from(id: LanguageIdentifier) -> Self

Converts to this type from the input type.
source§

impl From<Locale> for LanguageIdentifier

source§

fn from(loc: Locale) -> Self

Converts to this type from the input type.
source§

impl From<Option<Region>> for LanguageIdentifier

§Examples

use icu::locid::{langid, subtags::region, LanguageIdentifier};

assert_eq!(
    LanguageIdentifier::from(Some(region!("US"))),
    langid!("und-US")
);
source§

fn from(region: Option<Region>) -> Self

Converts to this type from the input type.
source§

impl From<Option<Script>> for LanguageIdentifier

§Examples

use icu::locid::{langid, subtags::script, LanguageIdentifier};

assert_eq!(
    LanguageIdentifier::from(Some(script!("latn"))),
    langid!("und-Latn")
);
source§

fn from(script: Option<Script>) -> Self

Converts to this type from the input type.
source§

impl FromStr for LanguageIdentifier

source§

type Err = ParserError

The associated error which can be returned from parsing.
source§

fn from_str(source: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for LanguageIdentifier

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 for LanguageIdentifier

source§

fn eq(&self, other: &LanguageIdentifier) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Writeable for LanguageIdentifier

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§

fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering

Compares the contents of this Writeable to the given bytes without allocating a String to hold the Writeable contents. Read more
source§

impl Eq for LanguageIdentifier

source§

impl StructuralPartialEq for LanguageIdentifier

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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 T
where T: Clone,

source§

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 T
where 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 T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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