Skip to main content

Locale

Struct Locale 

Source
pub struct Locale {
    pub language_code: String,
    pub script_code: Option<String>,
    pub country_code: Option<String>,
}
Expand description

An identifier used to select a user’s language and formatting preferences.

This represents a Unicode Language Identifier (i.e. without Locale extensions), except variants are not supported.

Locales are canonicalized according to the “preferred value” entries in the IANA Language Subtag Registry. For example, const Locale('he') and const Locale('iw') are equal and both have the language_code he, because iw is a deprecated language subtag that was replaced by the subtag he.

See also:

  • Localizations, which provides localized resources to the widget tree.

Fields§

§language_code: String

The primary language subtag for the locale.

This must not be null. It may be ‘und’, representing ‘undefined’.

This is expected to be string registered in the IANA Language Subtag Registry with the type “language”. The string specified must match the case of the string in the registry.

Language subtags that are deprecated in the registry and have a preferred code are changed to their preferred code. For example, const Locale('he') and const Locale('iw') are equal, and both have the language_code he, because iw is a deprecated language subtag that was replaced by the subtag he.

This must be a valid Unicode Language subtag as listed in Unicode CLDR supplemental data.

See also:

§script_code: Option<String>

The script subtag for the locale.

This may be null, indicating that there is no specified script subtag.

This must be a valid Unicode Language Identifier script subtag as listed in Unicode CLDR supplemental data.

See also:

§country_code: Option<String>

The region subtag for the locale.

This may be null, indicating that there is no specified region subtag.

This is expected to be string registered in the IANA Language Subtag Registry with the type “region”. The string specified must match the case of the string in the registry.

Region subtags that are deprecated in the registry and have a preferred code are changed to their preferred code. For example, const Locale('de', 'DE') and const Locale('de', 'DD') are equal, and both have the country_code DE, because DD is a deprecated language subtag that was replaced by the subtag DE.

See also:

Implementations§

Source§

impl Locale

Source

pub fn new(language_code: impl Into<String>) -> Locale

Creates a new Locale object. The first argument is the primary language subtag, the second is the region (also referred to as ‘country’) subtag.

For example:

let swiss_french = Locale::new("fr").country_code("CH");
let canadian_french = Locale::new("fr").country_code("CA");

The primary language subtag must not be null. The region subtag is optional. When there is no region/country subtag, the parameter should be omitted or set to None instead of an empty string.

The subtag values are case sensitive and must be one of the valid subtags according to CLDR supplemental data: language, region. The primary language subtag must be at least two and at most eight lowercase letters, but not four letters. The region subtag must be two uppercase letters or three digits. See the Unicode Language Identifier specification.

Validity is not checked by default, but some methods may throw away invalid data.

Source

pub fn from_subtags() -> Locale

Creates a new Locale object from its language, script, and region subtags, each optional. The language defaults to und.

The keyword arguments specify the subtags of the Locale.

The subtag values are case sensitive and must be valid subtags according to CLDR supplemental data: language, script and region for each of language_code, script_code and country_code respectively.

The country_code subtag is optional. When there is no country subtag, the parameter should be omitted instead of using an empty string.

Validity is not checked by default, but some methods may throw away invalid data.

Source

pub fn language_code(self, language_code: impl Into<String>) -> Locale

Dart Locale.fromSubtags(languageCode:); a deprecated subtag becomes its preferred value.

Source

pub fn script_code(self, script_code: impl Into<String>) -> Locale

Dart Locale.fromSubtags(scriptCode:).

Source

pub fn country_code(self, country_code: impl Into<String>) -> Locale

Dart Locale(languageCode, countryCode) / Locale.fromSubtags(countryCode:); a deprecated subtag becomes its preferred value, and '' is no country at all.

Source

pub fn to_language_tag(&self) -> String

Returns a syntactically valid Unicode BCP47 Locale Identifier.

Some examples of such identifiers: “en”, “es-419”, “hi-Deva-IN” and “zh-Hans-CN”. See http://www.unicode.org/reports/tr35/ for technical details.

Trait Implementations§

Source§

impl Clone for Locale

Source§

fn clone(&self) -> Locale

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Locale

Source§

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

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

impl Display for Locale

Source§

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

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

impl Eq for Locale

Source§

impl Hash for Locale

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 Locale

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Locale

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,