pub struct MultiLoader<T = Box<dyn Loader + Sync + Send>> { /* private fields */ }Expand description
A loader comprised of other loaders.
This loader allows for loaders with multiple sources to be used from a single one, instead of a multiple of them.
The idea behind this loader is to allow for the scenario where you depend on crates that have their own loader (think of protocol crates which are dependencies of a frontend -> the frontend needs to know each of the protocol’s messages and be able to display them). Using a multiloader allows you to query multiple localization sources from one single source.
Note that a MultiLoader is most useful where each of your fluent modules
is specially namespaced to avoid name collisions.
§Usage
use fluent_templates::{ArcLoader, StaticLoader, MultiLoader, Loader};
use unic_langid::{LanguageIdentifier, langid};
const US_ENGLISH: LanguageIdentifier = langid!("en-US");
const CHINESE: LanguageIdentifier = langid!("zh-CN");
fluent_templates::static_loader! {
static LOCALES = {
locales: "./tests/locales",
fallback_language: "en-US",
// Removes unicode isolating marks around arguments, you typically
// should only set to false when testing.
customise: |bundle| bundle.set_use_isolating(false),
};
}
fn main() {
let cn_loader = ArcLoader::builder("./tests/locales", CHINESE)
.customize(|bundle| bundle.set_use_isolating(false))
.build()
.unwrap();
let mut multiloader = MultiLoader::from_iter([
Box::new(&*LOCALES) as Box<dyn Loader>,
]);
multiloader.push_back(Box::new(cn_loader) as Box<dyn Loader>);
assert_eq!("Hello World!", multiloader.lookup(&US_ENGLISH, "hello-world"));
assert_eq!("儿", multiloader.lookup(&CHINESE, "exists"));
}§Order of search
The one that is inserted first is also the one searched first.
Implementations§
Source§impl<T> MultiLoader<T>
impl<T> MultiLoader<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a MultiLoader without any loaders.
Sourcepub fn from_iter(iter: impl IntoIterator<Item = T>) -> Self
pub fn from_iter(iter: impl IntoIterator<Item = T>) -> Self
Creates a MultiLoader from an iterator of loaders.
Sourcepub fn push_front(&mut self, loader: T)
pub fn push_front(&mut self, loader: T)
Pushes a loader in front of all the others in terms of precedence.
Trait Implementations§
Source§impl<T> Default for MultiLoader<T>
impl<T> Default for MultiLoader<T>
Source§impl<T: Loader> Loader for MultiLoader<T>
impl<T: Loader> Loader for MultiLoader<T>
Source§fn lookup_complete(
&self,
lang: &LanguageIdentifier,
text_id: &str,
args: Option<&HashMap<Cow<'static, str>, FluentValue<'_>>>,
) -> String
fn lookup_complete( &self, lang: &LanguageIdentifier, text_id: &str, args: Option<&HashMap<Cow<'static, str>, FluentValue<'_>>>, ) -> String
text_id for lang in Fluent, using any args if provided.Source§fn try_lookup_complete(
&self,
lang: &LanguageIdentifier,
text_id: &str,
args: Option<&HashMap<Cow<'static, str>, FluentValue<'_>>>,
) -> Option<String>
fn try_lookup_complete( &self, lang: &LanguageIdentifier, text_id: &str, args: Option<&HashMap<Cow<'static, str>, FluentValue<'_>>>, ) -> Option<String>
text_id for lang in Fluent, using any args if provided.Source§fn locales(&self) -> Box<dyn Iterator<Item = &LanguageIdentifier> + '_>
fn locales(&self) -> Box<dyn Iterator<Item = &LanguageIdentifier> + '_>
Source§fn lookup(&self, lang: &LanguageIdentifier, text_id: &str) -> String
fn lookup(&self, lang: &LanguageIdentifier, text_id: &str) -> String
text_id for lang in Fluent.Source§fn lookup_with_args(
&self,
lang: &LanguageIdentifier,
text_id: &str,
args: &HashMap<Cow<'static, str>, FluentValue<'_>>,
) -> String
fn lookup_with_args( &self, lang: &LanguageIdentifier, text_id: &str, args: &HashMap<Cow<'static, str>, FluentValue<'_>>, ) -> String
text_id for lang with args in Fluent.Source§fn try_lookup(&self, lang: &LanguageIdentifier, text_id: &str) -> Option<String>
fn try_lookup(&self, lang: &LanguageIdentifier, text_id: &str) -> Option<String>
text_id for lang in Fluent.Source§fn try_lookup_with_args(
&self,
lang: &LanguageIdentifier,
text_id: &str,
args: &HashMap<Cow<'static, str>, FluentValue<'_>>,
) -> Option<String>
fn try_lookup_with_args( &self, lang: &LanguageIdentifier, text_id: &str, args: &HashMap<Cow<'static, str>, FluentValue<'_>>, ) -> Option<String>
text_id for lang with args in Fluent.