Struct icu_locid::Locale[][src]

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 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: LanguageIdentifierextensions: Extensions

Implementations

A constructor which takes a utf8 slice, parses it and produces a well-formed Locale.

Examples

use icu::locid::Locale;

let loc = Locale::from_bytes("en-US-u-hc-h12".as_bytes())
    .expect("Parsing failed.");

assert_eq!(loc.to_string(), "en-US-u-hc-h12");

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

Examples

use icu::locid::Locale;

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

assert_eq!(Locale::canonicalize("pL_latn_pl-U-HC-H12"), Ok("pl-Latn-PL-u-hc-h12".to_string()));

Accessor method for unicode extensions.

Examples

use icu::locid::{extensions::unicode::Key, Locale};

let loc = Locale::from_bytes("en-US-u-hc-h12".as_bytes())
    .expect("Parsing failed.");
let key = "hc".parse::<Key>()
    .expect("Invalid key.");
let ext = loc.get_unicode_extension(&key)
    .expect("Extension not defined");
assert_eq!(ext.to_string(), "h12");

Trait Implementations

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

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

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.