pub trait FluentLocalizer {
// Required methods
fn localize<'a>(
&self,
id: &str,
args: Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String>;
fn localize_in_domain<'a>(
&self,
domain: &str,
id: &str,
args: Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String>;
// Provided method
fn with_lookup(
&self,
f: &mut dyn FnMut(&mut dyn for<'a> FnMut(&str, &str, Option<&HashMap<&str, FluentValue<'a>>>) -> Option<String>),
) { ... }
}Expand description
Runtime context that can resolve Fluent message IDs.
This is the manager-facing replacement for the removed context-free global lookup. Managers and framework adapters implement this trait so callers can keep typed message values while passing the active localization context explicitly.
§Implementing FluentLocalizer
Custom localizers should either use the default Self::with_lookup
implementation or override it to provide one render-scoped snapshot. If
with_lookup(...) is overridden, it must invoke the callback exactly once
before returning. Failing to do so is a logic error and will panic in
FluentLocalizerExt::localize_message and
FluentLocalizerExt::try_localize_message.
Required Methods§
Sourcefn localize<'a>(
&self,
id: &str,
args: Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String>
fn localize<'a>( &self, id: &str, args: Option<&HashMap<&str, FluentValue<'a>>>, ) -> Option<String>
Localizes a message by ID using the localizer’s default lookup behavior.
Sourcefn localize_in_domain<'a>(
&self,
domain: &str,
id: &str,
args: Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String>
fn localize_in_domain<'a>( &self, domain: &str, id: &str, args: Option<&HashMap<&str, FluentValue<'a>>>, ) -> Option<String>
Localizes a message by ID within a specific domain.
Provided Methods§
Sourcefn with_lookup(
&self,
f: &mut dyn FnMut(&mut dyn for<'a> FnMut(&str, &str, Option<&HashMap<&str, FluentValue<'a>>>) -> Option<String>),
)
fn with_lookup( &self, f: &mut dyn FnMut(&mut dyn for<'a> FnMut(&str, &str, Option<&HashMap<&str, FluentValue<'a>>>) -> Option<String>), )
Runs a group of lookups against one render-scoped localization view.
Implementations must invoke the callback exactly once, must not call it
after with_lookup(...) returns, and should provide a stable lookup
snapshot for the duration of that callback. The extension methods rely
on this contract when rendering nested typed messages.
The callback is the only supported lookup path inside a typed message
render. Custom FluentMessage implementations must not re-enter the
same localizer for language selection or other lock-taking operations
while this callback is active.
The default implementation delegates each lookup independently. Managers with mutable language selection should override this to hold the relevant lock or snapshot for the whole callback.
§Example
struct MyLocalizer;
impl MyLocalizer {
fn lookup<'a>(
&self,
domain: &str,
id: &str,
_args: Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String> {
Some(format!("{domain}:{id}"))
}
}
impl FluentLocalizer for MyLocalizer {
fn localize<'a>(
&self,
id: &str,
args: Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String> {
self.localize_in_domain(env!("CARGO_PKG_NAME"), id, args)
}
fn localize_in_domain<'a>(
&self,
domain: &str,
id: &str,
args: Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String> {
self.lookup(domain, id, args)
}
fn with_lookup(
&self,
f: &mut dyn FnMut(
&mut dyn for<'a> FnMut(
&str,
&str,
Option<&HashMap<&str, FluentValue<'a>>>,
) -> Option<String>,
),
) {
let mut lookup =
|domain: &str, id: &str, args: Option<&HashMap<&str, FluentValue<'_>>>| {
self.localize_in_domain(domain, id, args)
};
f(&mut lookup);
}
}