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>
impl<'a> Locale<'a>
Sourcepub fn new(languages: &'a Languages, default: &'a str) -> Result<Self, Error>
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).
Sourcepub fn with(
languages: &'a Languages,
default: impl Into<Cow<'a, str>>,
fallback: Option<impl Into<Cow<'a, str>>>,
) -> Self
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.
Sourcepub fn default(&self) -> &str
pub fn default(&self) -> &str
The name of the language texts are read from first.
PHP: $locale->default.
Sourcepub fn fallback(&self) -> Option<&str>
pub fn fallback(&self) -> Option<&str>
The name of the language read for keys the default one lacks.
PHP: $locale->fallback.
Sourcepub fn set_default(&mut self, name: &str) -> Result<&mut Self, Error>
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).
Sourcepub fn set_fallback(&mut self, name: &str) -> Result<&mut Self, Error>
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).
Sourcepub fn text<'s>(
&'s self,
key: &str,
missing: Missing<'s>,
placeholders: &[(&str, &str)],
) -> Result<Option<Cow<'s, str>>, Error>
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.
Sourcepub 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>
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.
Sourcepub fn translations(&self) -> Result<&'a Translations, Error>
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().