Struct icu_locid::LanguageIdentifier[][src]

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

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.

Returns the default undefined language “und”. Same as default(), but is const.

Examples

use icu::locid::LanguageIdentifier;

const langid: LanguageIdentifier = LanguageIdentifier::und();
assert_eq!(LanguageIdentifier::default(), langid);
assert_eq!("und", langid.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()));

Trait Implementations

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

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

Performs the conversion.

Performs the conversion.

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 returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

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

This method tests for !=.

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

This method tests for !=.

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

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

Writes bytes to the given sink. Errors from the sink are bubbled up.

Returns a hint for the number of bytes that will be written to the sink. 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

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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.