locale_config

Struct LanguageRange

Source
pub struct LanguageRange<'a> { /* private fields */ }
Expand description

Language and culture identifier.

This object holds a RFC4647 extended language range.

The internal data may be owned or shared from object with lifetime 'a. The lifetime can be extended using the into_static() method, which internally clones the data as needed.

§Syntax

The range is composed of --separated alphanumeric subtags, possibly replaced by *s. It might be empty.

In agreement with RFC4647, this object only requires that the tag matches:

language_tag = (alpha{1,8} | "*")
               ("-" (alphanum{1,8} | "*"))*

The exact interpretation is up to the downstream localization provider, but it expected that it will be matched against a normalized RFC5646 language tag, which has the structure:

language_tag    = language
                  ("-" script)?
                  ("-" region)?
                  ("-" variant)*
                  ("-" extension)*
                  ("-" private)?

language        = alpha{2,3} ("-" alpha{3}){0,3}

script          = aplha{4}

region          = alpha{2}
                | digit{3}

variant         = alphanum{5,8}
                | digit alphanum{3}

extension       = [0-9a-wyz] ("-" alphanum{2,8})+

private         = "x" ("-" alphanum{1,8})+
  • language is an ISO639 2-letter or, where not defined, 3-letter code. A code for macro-language might be followed by code of specific dialect.
  • script is an ISO15924 4-letter code.
  • region is either an ISO3166 2-letter code or, for areas other than countries, UN M.49 3-digit numeric code.
  • variant is a string indicating variant of the language.
  • extension and private define additional options. The private part has same structure as the Unicode -u- extension. Available options are documented for the facets that use them.

The values obtained by inspecting the system are normalized according to those rules.

The content will be case-normalized as recommended in RFC5646 §2.1.1, namely:

  • language is written in lowercase,
  • script is written with first capital,
  • country is written in uppercase and
  • all other subtags are written in lowercase.

When detecting system configuration, additional options that may be generated under the -u- extension currently are:

  • cf — Currency format (account for parenthesized negative values, standard for minus sign).
  • fw — First day of week (mon to sun).
  • hc — Hour cycle (h12 for 1–12, h23 for 0–23).
  • ms — Measurement system (metric or ussystem).
  • nu — Numbering system—only decimal systems are currently used.
  • va — Variant when locale is specified in Unix format and the tag after @ does not correspond to any variant defined in Language subtag registry.

And under the -x- extension, following options are defined:

  • df — Date format:

    • iso: Short date should be in ISO format of yyyy-MM-dd.

    For example -df-iso.

  • dm — Decimal separator for monetary:

    Followed by one or more Unicode codepoints in hexadecimal. For example -dm-002d means to use comma.

  • ds — Decimal separator for numbers:

    Followed by one or more Unicode codepoints in hexadecimal. For example -ds-002d means to use comma.

  • gm — Group (thousand) separator for monetary:

    Followed by one or more Unicode codepoints in hexadecimal. For example -dm-00a0 means to use non-breaking space.

  • gs — Group (thousand) separator for numbers:

    Followed by one or more Unicode codepoints in hexadecimal. For example -ds-00a0 means to use non-breaking space.

  • ls — List separator:

    Followed by one or more Unicode codepoints in hexadecimal. For example, -ds-003b means to use a semicolon.

Implementations§

Source§

impl<'a> LanguageRange<'a>

Source

pub fn new(lt: &'a str) -> Result<LanguageRange<'_>, Error>

Construct LanguageRange from string, with normalization.

LanguageRange must follow the RFC4647 syntax. It will be case-normalized as recommended in RFC5646 §2.1.1, namely:

  • language, if recognized, is written in lowercase,
  • script, if recognized, is written with first capital,
  • country, if recognized, is written in uppercase and
  • all other subtags are written in lowercase.
Source

pub fn invariant() -> LanguageRange<'static>

Return LanguageRange for the invariant locale.

Invariant language is identified simply by empty string.

Source

pub fn into_static(self) -> LanguageRange<'static>

Clone the internal data to extend lifetime.

Source

pub fn to_shared(&'a self) -> Self

Create new instance sharing the internal data.

Source

pub fn from_unix(s: &str) -> Result<LanguageRange<'static>, Error>

Create language tag from Unix/Linux/GNU locale tag.

Unix locale tags have the form

language [ _ region ] [ . encoding ] [ @ variant ]

The language and region have the same format as RFC5646. Encoding is not relevant here, since Rust always uses Utf-8. That leaves variant, which is unfortunately rather free-form. So this function will translate known variants to corresponding RFC5646 subtags and represent anything else with Unicode POSIX variant (-u-va-) extension.

Note: This function is public here for benefit of applications that may come across this kind of tags from other sources than system configuration.

Trait Implementations§

Source§

impl<'a> AsRef<str> for LanguageRange<'a>

Source§

fn as_ref(&self) -> &str

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

impl<'a> Clone for LanguageRange<'a>

Source§

fn clone(&self) -> LanguageRange<'a>

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<'a> Debug for LanguageRange<'a>

Source§

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

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

impl<'a> Display for LanguageRange<'a>

Source§

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

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

impl<'a> From<LanguageRange<'a>> for Locale

Source§

fn from(t: LanguageRange<'a>) -> Locale

Converts to this type from the input type.
Source§

impl<'a> Hash for LanguageRange<'a>

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<'a> PartialEq for LanguageRange<'a>

Source§

fn eq(&self, other: &LanguageRange<'a>) -> 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<'a> Eq for LanguageRange<'a>

Source§

impl<'a> StructuralPartialEq for LanguageRange<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for LanguageRange<'a>

§

impl<'a> RefUnwindSafe for LanguageRange<'a>

§

impl<'a> Send for LanguageRange<'a>

§

impl<'a> Sync for LanguageRange<'a>

§

impl<'a> Unpin for LanguageRange<'a>

§

impl<'a> UnwindSafe for LanguageRange<'a>

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.