pub trait I18nPlugin: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn translate(
&self,
key: &str,
locale: &str,
vars: &TransVars,
) -> Result<String, I18nError>;
fn supported_locales(&self) -> Vec<String>;
fn fallback_locale(&self) -> String;
// Provided methods
fn has_locale(&self, locale: &str) -> bool { ... }
fn negotiate(&self, requested: &str) -> String { ... }
}Expand description
A plugin that translates keys into localized strings.
Host functions i18n.t(key, vars) and i18n.locales() call
through to the currently registered implementation. The host
layer is responsible for negotiating the request’s locale from
headers / session / URL before calling translate.
Required Methods§
Sourcefn translate(
&self,
key: &str,
locale: &str,
vars: &TransVars,
) -> Result<String, I18nError>
fn translate( &self, key: &str, locale: &str, vars: &TransVars, ) -> Result<String, I18nError>
Translate key into locale, interpolating vars.
Returns Ok(rendered) in the overwhelming majority of cases,
including when the key is unknown — backends render missing
keys as a safe marker (the key itself, or "{key}") rather
than returning an error, because a missing translation must
never take down a UI.
Returns Err(I18nError) only for genuine backend failures:
a locale that was promised but failed to load, a format
error, a backend in a transiently unavailable state.
Sourcefn supported_locales(&self) -> Vec<String>
fn supported_locales(&self) -> Vec<String>
All locales this plugin can serve, as BCP 47 tags
("en", "en-US", "fr-CA", "zh-Hant-TW"). The order is
display order, not preference order; callers negotiate via
negotiate.
Sourcefn fallback_locale(&self) -> String
fn fallback_locale(&self) -> String
The locale to use when the caller asks for one that is not
in supported_locales() and no partial match negotiated. A
BCP 47 tag; MUST appear in supported_locales().
Provided Methods§
Sourcefn has_locale(&self, locale: &str) -> bool
fn has_locale(&self, locale: &str) -> bool
Whether the plugin recognises the given locale tag exactly
(case-insensitive). The default implementation linearly
scans supported_locales(); backends that maintain a fast
lookup structure override this.
Sourcefn negotiate(&self, requested: &str) -> String
fn negotiate(&self, requested: &str) -> String
Negotiate a requested locale down to a locale the plugin can actually serve, per BCP 47 Lookup (RFC 4647 §3.4).
The default implementation implements the standard walk:
given "fr-CA", try "fr-CA", then "fr", then
fallback_locale(). Backends are free to override with a
richer matcher (Fluent, for example, has its own negotiation
that also considers script subtags).