Skip to main content

Locale

Struct Locale 

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

A view of Languages with a default and an optional fallback language.

Implementations§

Source§

impl<'a> Locale<'a>

Source

pub fn new(languages: &'a Languages, default: &'a str) -> Result<Self, Error>

A locale reading the default language, with no fallback.

Fails with Locale not found for an unknown language while exceptions are on.

PHP: new Locale($default).

Source

pub fn with( languages: &'a Languages, default: impl Into<Cow<'a, str>>, fallback: Option<impl Into<Cow<'a, str>>>, ) -> Self

A locale with this default and fallback language, without checking that they are registered.

PHP: assigning the public $default and $fallback properties.

Source

pub fn default(&self) -> &str

The name of the language texts are read from first.

PHP: $locale->default.

Source

pub fn fallback(&self) -> Option<&str>

The name of the language read for keys the default one lacks.

PHP: $locale->fallback.

Source

pub fn set_default(&mut self, name: &str) -> Result<&mut Self, Error>

Switches the default language.

Fails with Locale not found for an unknown language while exceptions are on.

PHP: $locale->setDefault($name).

Source

pub fn set_fallback(&mut self, name: &str) -> Result<&mut Self, Error>

Sets the language read for keys the default one lacks.

Fails with Locale not found for an unknown language while exceptions are on.

PHP: $locale->setFallback($name).

Source

pub fn text<'s>( &'s self, key: &str, missing: Missing<'s>, placeholders: &[(&str, &str)], ) -> Result<Option<Cow<'s, str>>, Error>

The translation of key, with its placeholders filled in.

The default language’s text, else the fallback’s, else missing; an empty text counts as missing (with exceptions on, a key neither has fails with Key named "<key>" not found). Each {{name}} of placeholders is then replaced in turn, so a value can itself hold a later placeholder. Ok(None) only for Missing::Null.

Without a fallback, the language named "" serves as the fallback.

PHP: $locale->getText($key, $default, $placeholders). PHP reads the language named "" without a fallback, and so does this.

Source

pub fn text_with<'s, N: AsRef<str>, V: AsRef<str>>( &'s self, key: &str, missing: Missing<'s>, placeholders: impl IntoIterator<Item = (N, V)>, ) -> Result<Option<Cow<'s, str>>, Error>

Locale::text, with the placeholders as any sequence of name and value pairs.

Source

pub fn translations(&self) -> Result<&'a Translations, Error>

Every text of the default language.

Fails with Error::Type when the default language is unknown or holds no translations.

PHP: $locale->getTranslations().

Trait Implementations§

Source§

impl<'a> Clone for Locale<'a>

Source§

fn clone(&self) -> Self

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

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Locale<'a>

§

impl<'a> Freeze for Locale<'a>

§

impl<'a> Move for Locale<'a>

§

impl<'a> RefUnwindSafe for Locale<'a>

§

impl<'a> Send for Locale<'a>

§

impl<'a> Sync for Locale<'a>

§

impl<'a> Unpin for Locale<'a>

§

impl<'a> UnsafeUnpin for Locale<'a>

§

impl<'a> UnwindSafe for Locale<'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, 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> 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> Same for T

Source§

type Output = T

Should always be Self
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, 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.