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<Self, LangError>
pub fn load( path: impl AsRef<str>, fallback: impl Into<String>, ) -> Result<Self, 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.