pub struct Translator { /* private fields */ }Expand description
Core translation engine.
Loads JSON translation files from a directory structure, pre-merges fallback translations, and provides lookup with interpolation and pluralization.
Implementations§
Source§impl Translator
impl Translator
Sourcepub fn load(
path: impl AsRef<str>,
fallback: impl Into<String>,
) -> Result<Translator, LangError>
pub fn load( path: impl AsRef<str>, fallback: impl Into<String>, ) -> Result<Translator, LangError>
Load translations from {path}/{locale}/*.json with the given fallback locale.
The fallback locale’s keys are pre-merged into every other locale so
runtime lookup is a single HashMap::get.
Sourcepub fn get(&self, locale: &str, key: &str, params: &[(&str, &str)]) -> String
pub fn get(&self, locale: &str, key: &str, params: &[(&str, &str)]) -> String
Look up a translation key with parameter interpolation.
Returns the translated string with :param placeholders replaced.
If the key is not found, returns the key itself (no panic, no Option).
Sourcepub fn choice(
&self,
locale: &str,
key: &str,
count: i64,
params: &[(&str, &str)],
) -> String
pub fn choice( &self, locale: &str, key: &str, count: i64, params: &[(&str, &str)], ) -> String
Look up a pluralized translation key.
Selects the correct plural form from pipe-separated values, then
applies parameter interpolation. A :count parameter is automatically
added with the string representation of count.
Auto Trait Implementations§
impl Freeze for Translator
impl RefUnwindSafe for Translator
impl Send for Translator
impl Sync for Translator
impl Unpin for Translator
impl UnsafeUnpin for Translator
impl UnwindSafe for Translator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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