locale_config

Struct Locale

Source
pub struct Locale { /* private fields */ }
Expand description

Locale configuration.

Users may accept several languages in some order of preference and may want to use rules from different culture for some particular aspect of the program behaviour, and operating systems allow them to specify this (to various extent).

The Locale objects represent the user configuration. They contain:

  • The primary LanguageRange.
  • Optional category-specific overrides.
  • Optional fallbacks in case data (usually translations) for the primary language are not available.

The set of categories is open-ended. The locale crate uses five well-known categories messages, numeric, time, collate and monetary, but some systems define additional ones (GNU Linux has additionally paper, name, address, telephone and measurement) and these are provided in the user default Locale and other libraries can use them.

Locale is represented by a ,-separated sequence of tags in LanguageRange syntax, where all except the first one may be preceded by category name and = sign.

The first tag indicates the default locale, the tags prefixed by category names indicate overrides for those categories and the remaining tags indicate fallbacks.

Note that a syntactically valid value of HTTP Accept-Language header is a valid Locale. Not the other way around though due to the presence of category selectors.

Implementations§

Source§

impl Locale

Source

pub fn user_default() -> Locale

Obtain the user default locale.

This is the locale indicated by operating environment.

Examples found in repository?
examples/show-user-locale.rs (line 4)
3
4
5
pub fn main() {
    println!("{}", locale_config::Locale::user_default());
}
Source

pub fn global_default() -> Locale

Obtain the global default locale.

The global default for current() locale. Defaults to user_default().

Source

pub fn set_global_default(lb: Locale)

Change the global default locale.

Setting this overrides the default for new threads and threads that didn’t do any locale-aware operation yet.

Source

pub fn current() -> Locale

Obtain the current locale of current thread.

Defaults to global_default() on first use in each thread.

Source

pub fn set_current(lb: Locale)

Change the current locale of current thread.

Source

pub fn new(s: &str) -> Result<Locale, Error>

Construct locale from the string representation.

Locale is represented by a ,-separated sequence of tags in LanguageRange syntax, where all except the first one may be preceded by category name and = sign.

The first tag indicates the default locale, the tags prefixed by category names indicate overrides for those categories and the remaining tags indicate fallbacks.

Source

pub fn invariant() -> Locale

Construct invariant locale.

Invariant locale is represented simply with empty string.

Source

pub fn add(&mut self, tag: &LanguageRange<'_>)

Append fallback language tag.

Adds fallback to the end of the list.

Source

pub fn add_category(&mut self, category: &str, tag: &LanguageRange<'_>)

Append category override.

Appending new override for a category that already has one will not replace the existing override. This might change in future.

Source

pub fn tags<'a>(&'a self) -> Tags<'a>

Iterate over LanguageRanges in this Locale.

Returns tuples of optional category (as string) and corresponding LanguageRange. All tags in the list are returned, in order of preference.

The iterator is guaranteed to return at least one value.

Source

pub fn tags_for<'a, 'c>(&'a self, category: &'c str) -> TagsFor<'a, 'c>

Iterate over LanguageRanges in this Locale applicable to given category.

Returns LanguageRanges in the Locale that are applicable to provided category. The tags are returned in order of preference, which means the category-specific ones first and then the generic ones.

The iterator is guaranteed to return at least one value.

Trait Implementations§

Source§

impl AsRef<str> for Locale

Locale is specified by a string tag. This is the way to access it.

Source§

fn as_ref(&self) -> &str

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

impl Clone for Locale

Source§

fn clone(&self) -> Locale

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

Source§

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

Converts to this type from the input type.
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

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 Eq for Locale

Source§

impl StructuralPartialEq for Locale

Auto Trait Implementations§

§

impl Freeze for Locale

§

impl RefUnwindSafe for Locale

§

impl Send for Locale

§

impl Sync for Locale

§

impl Unpin for Locale

§

impl UnwindSafe for Locale

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.