pub struct Translator { /* private fields */ }Expand description
A lightweight, request-scoped translation helper.
Translator stores an interned locale identifier and forwards lookups to
the global Lang store without mutating the process-wide active locale.
This makes it a good fit for web handlers, jobs, and other code paths
where locale is part of the input rather than part of global application
state.
Cloning a Translator is cheap — the locale is held as a &'static str,
so the operation is a pointer copy and no allocation occurs.
§Examples
use lang_lib::{Lang, Translator};
Lang::load_from("en", "tests/fixtures/locales").unwrap();
let translator = Translator::new("en");
let title = translator.translate_with_fallback("welcome", "Welcome");
assert_eq!(title, "Welcome");Implementations§
Source§impl Translator
impl Translator
Sourcepub fn translate<'a>(&self, key: &'a str) -> Cow<'a, str>
pub fn translate<'a>(&self, key: &'a str) -> Cow<'a, str>
Translates a key using this translator’s locale.
The returned value borrows directly into the interned translation
store on the hit path and into key on the complete-miss path. Both
outcomes are zero-allocation.
Sourcepub fn translate_with_fallback<'a>(
&self,
key: &'a str,
fallback: &'a str,
) -> Cow<'a, str>
pub fn translate_with_fallback<'a>( &self, key: &'a str, fallback: &'a str, ) -> Cow<'a, str>
Translates a key using this translator’s locale and an inline fallback.
The returned value borrows directly into the interned translation
store on the hit path, into fallback when the lookup misses, and
into key only if no fallback resolves either. All three outcomes
are zero-allocation.
Trait Implementations§
Source§impl Clone for Translator
impl Clone for Translator
Source§fn clone(&self) -> Translator
fn clone(&self) -> Translator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Translator
impl Debug for Translator
Source§impl PartialEq for Translator
impl PartialEq for Translator
Source§fn eq(&self, other: &Translator) -> bool
fn eq(&self, other: &Translator) -> bool
self and other values to be equal, and is used by ==.impl Copy for Translator
impl Eq for Translator
impl StructuralPartialEq for Translator
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.