pub trait FluentLocalizer {
// Required methods
fn localize<'a>(
&self,
id: StaticFluentEntryId,
args: Option<&'a FluentArgs<'a>>,
) -> Option<String>;
fn localize_in_domain<'a>(
&self,
domain: StaticFluentDomain,
id: StaticFluentEntryId,
args: Option<&'a FluentArgs<'a>>,
) -> Option<String>;
// Provided method
fn with_lookup(&self, f: &mut dyn FnMut(&mut FluentLocalizerLookup<'_>)) { ... }
}Expand description
Runtime context that resolves Fluent message IDs for typed message values.
Managers and framework adapters implement this trait so callers can pass 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: StaticFluentEntryId,
args: Option<&'a FluentArgs<'a>>,
) -> Option<String>
fn localize<'a>( &self, id: StaticFluentEntryId, args: Option<&'a FluentArgs<'a>>, ) -> Option<String>
Localizes a validated static message ID using the localizer’s default lookup behavior.
Sourcefn localize_in_domain<'a>(
&self,
domain: StaticFluentDomain,
id: StaticFluentEntryId,
args: Option<&'a FluentArgs<'a>>,
) -> Option<String>
fn localize_in_domain<'a>( &self, domain: StaticFluentDomain, id: StaticFluentEntryId, args: Option<&'a FluentArgs<'a>>, ) -> Option<String>
Localizes a validated static message ID within a validated static domain.
Provided Methods§
Sourcefn with_lookup(&self, f: &mut dyn FnMut(&mut FluentLocalizerLookup<'_>))
fn with_lookup(&self, f: &mut dyn FnMut(&mut FluentLocalizerLookup<'_>))
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;
fn lookup(
domain: StaticFluentDomain,
id: StaticFluentEntryId,
_args: Option<&FluentArgs<'_>>,
) -> Option<String> {
Some(format!("{domain}:{id}"))
}
impl FluentLocalizer for MyLocalizer {
fn localize<'a>(
&self,
id: StaticFluentEntryId,
args: Option<&FluentArgs<'a>>,
) -> Option<String> {
self.localize_in_domain(StaticFluentDomain::from_package_name(env!("CARGO_PKG_NAME")), id, args)
}
fn localize_in_domain<'a>(
&self,
domain: StaticFluentDomain,
id: StaticFluentEntryId,
args: Option<&FluentArgs<'a>>,
) -> Option<String> {
lookup(domain, id, args)
}
fn with_lookup(
&self,
f: &mut dyn FnMut(&mut es_fluent::FluentLocalizerLookup<'_>),
) {
let mut lookup = lookup;
f(&mut lookup);
}
}Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".