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, subtags::*};

let li: LanguageIdentifier = "en-US".parse().expect("Failed to parse.");

assert_eq!(li.language, "en".parse::<Language>().unwrap());
assert_eq!(li.script, None);
assert_eq!(li.region.unwrap(), "US".parse::<Region>().unwrap());
assert_eq!(li.variants.len(), 0);
assert_eq!(li.to_string(), "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, subtags::*};

let li: LanguageIdentifier = "eN_latn_Us-Valencia".parse().expect("Failed to parse.");

assert_eq!(li.language, "en".parse::<Language>().unwrap());
assert_eq!(li.script, "Latn".parse::<Script>().ok());
assert_eq!(li.region, "US".parse::<Region>().ok());
assert_eq!(li.variants.get(0), "valencia".parse::<Variant>().ok().as_ref());

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

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");

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.

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

Examples
use icu::locid::LanguageIdentifier;

assert_eq!(LanguageIdentifier::default(), LanguageIdentifier::UND);
assert_eq!("und", LanguageIdentifier::UND.to_string());

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())
);

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_eq!(a, a_langid.to_string());
    assert!(a_langid.strict_cmp(a.as_bytes()) == Ordering::Equal);
    assert!(a_langid.strict_cmp(b.as_bytes()) == Ordering::Less);
}

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()
);

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 structucal comparison.

Examples
use icu::locid::LanguageIdentifier;
use std::cmp::Ordering;

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));
}

Trait Implementations

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

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Convert from a LanguageIdentifier to an LSR tuple.

Examples

use icu::locid::LanguageIdentifier;
use icu::locid::{subtags_language as language, subtags_region as region, subtags_script as script, langid};

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")));

Converts to this type from the input type.

Convert from an LSR tuple to a LanguageIdentifier.

Examples

use icu::locid::LanguageIdentifier;
use icu::locid::{subtags_language as language, subtags_region as region, subtags_script as script};

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, lang);
assert_eq!(li.script.unwrap(), script);
assert_eq!(li.region.unwrap(), region);
assert_eq!(li.variants.len(), 0);
assert_eq!(li.to_string(), "en-Latn-US");

Converts to this type from the input type.

Examples

use icu::locid::subtags_language as language;
use icu::locid::LanguageIdentifier;

let language = language!("en");
let li = LanguageIdentifier::from(language);

assert_eq!(li.language, language);
assert_eq!(li.to_string(), "en");

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Examples

use icu::locid::subtags_region as region;
use icu::locid::LanguageIdentifier;

let region = region!("US");
let li = LanguageIdentifier::from(Some(region));

assert_eq!(li.region.unwrap(), region);
assert_eq!(li.to_string(), "und-US");

Converts to this type from the input type.

Examples

use icu::locid::subtags_script as script;
use icu::locid::LanguageIdentifier;

let script = script!("latn");
let li = LanguageIdentifier::from(Some(script));

assert_eq!(li.script.unwrap(), script);
assert_eq!(li.to_string(), "und-Latn");

Converts to this type from the input type.

The associated error which can be returned from parsing.

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

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

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

Returns a hint for the number of bytes that will be written to the sink. Read more

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

Creates a new String with the data from this Writeable. Like ToString, but smaller and faster. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.