Struct mas_i18n::Translator

source ·
pub struct Translator { /* private fields */ }
Expand description

A translator for a set of translations.

Implementations§

source§

impl Translator

source

pub fn new(translations: HashMap<DataLocale, TranslationTree>) -> Self

Create a new translator from a set of translations.

source

pub fn load_from_path(path: &Utf8Path) -> Result<Self, LoadError>

Load a set of translations from a directory.

The directory should contain one JSON file per locale, with the locale being the filename without the extension, e.g. en-US.json.

§Parameters
  • path - The path to load from.
§Errors

Returns an error if the directory cannot be read, or if any of the files cannot be parsed.

source

pub fn message_with_fallback( &self, locale: DataLocale, key: &str, ) -> Option<(&Message, DataLocale)>

Get a message from the tree by key, with locale fallback.

Returns the message and the locale it was found in. If the message is not found, returns None.

§Parameters
  • locale - The locale to use.
  • key - The key to look up, which is a dot-separated path.
source

pub fn message( &self, locale: &DataLocale, key: &str, ) -> Result<&Message, DataError>

Get a message from the tree by key.

§Parameters
  • locale - The locale to use.
  • key - The key to look up, which is a dot-separated path.
§Errors

Returns an error if the requested locale is not found, or if the requested key is not found.

source

pub fn plural_with_fallback( &self, locale: DataLocale, key: &str, count: usize, ) -> Option<(&Message, DataLocale)>

Get a plural message from the tree by key, with locale fallback.

Returns the message and the locale it was found in. If the message is not found, returns None.

§Parameters
  • locale - The locale to use.
  • key - The key to look up, which is a dot-separated path.
  • count - The count to use for pluralization.
source

pub fn plural( &self, locale: &DataLocale, key: &str, count: usize, ) -> Result<&Message, PluralsError>

Get a plural message from the tree by key.

§Parameters
  • locale - The locale to use.
  • key - The key to look up, which is a dot-separated path.
  • count - The count to use for pluralization.
§Errors

Returns an error if the requested locale is not found, or if the requested key is not found.

source

pub fn and_list<'a, W: Writeable + 'a, I: Iterator<Item = W> + Clone + 'a>( &'a self, locale: &DataLocale, items: I, ) -> Result<String, ListError>

Format a list of items with the “and” conjunction.

§Parameters
  • locale - The locale to use.
  • items - The items to format.
§Errors

Returns an error if the requested locale is not found.

source

pub fn or_list<'a, W: Writeable + 'a, I: Iterator<Item = W> + Clone + 'a>( &'a self, locale: &DataLocale, items: I, ) -> Result<String, ListError>

Format a list of items with the “or” conjunction.

§Parameters
  • locale - The locale to use.
  • items - The items to format.
§Errors

Returns an error if the requested locale is not found.

source

pub fn relative_date( &self, locale: &DataLocale, days: i64, ) -> Result<String, RelativeTimeError>

Format a relative date

§Parameters
  • locale - The locale to use.
  • days - The number of days to format, where 0 = today, 1 = tomorrow, -1 = yesterday, etc.
§Errors

Returns an error if the requested locale is not found.

source

pub fn short_time<T: IsoTimeInput>( &self, locale: &DataLocale, time: &T, ) -> Result<String, DateTimeError>

Format time

§Parameters
  • locale - The locale to use.
  • time - The time to format.
§Errors

Returns an error if the requested locale is not found.

source

pub fn available_locales(&self) -> Vec<&DataLocale>

Get a list of available locales.

source

pub fn has_locale(&self, locale: &DataLocale) -> bool

Check if a locale is available.

source

pub fn choose_locale( &self, iter: impl Iterator<Item = DataLocale>, ) -> DataLocale

Choose the best available locale from a list of candidates.

Trait Implementations§

source§

impl Debug for Translator

source§

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

Formats the value using the given formatter. Read more

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

source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T
where T: Send + Sync,