MultiLoader

Struct MultiLoader 

Source
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"));
}

The one that is inserted first is also the one searched first.

Implementations§

Source§

impl<T> MultiLoader<T>

Source

pub fn new() -> Self

Creates a MultiLoader without any loaders.

Source

pub fn from_iter(iter: impl IntoIterator<Item = T>) -> Self

Creates a MultiLoader from an iterator of loaders.

Source

pub fn push_front(&mut self, loader: T)

Pushes a loader in front of all the others in terms of precedence.

Source

pub fn push_back(&mut self, loader: T)

Pushes a loader at the back in terms of precedence.

Source

pub fn remove(&mut self, idx: usize) -> Option<T>

Pushes a loader at the back in terms of precedence.

Trait Implementations§

Source§

impl<T> Default for MultiLoader<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Look up 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>

Look up text_id for lang in Fluent, using any args if provided.
Source§

fn locales(&self) -> Box<dyn Iterator<Item = &LanguageIdentifier> + '_>

Returns an Iterator over the locales that are present.
Source§

fn lookup(&self, lang: &LanguageIdentifier, text_id: &str) -> String

Look up text_id for lang in Fluent.
Source§

fn lookup_with_args( &self, lang: &LanguageIdentifier, text_id: &str, args: &HashMap<Cow<'static, str>, FluentValue<'_>>, ) -> String

Look up text_id for lang with args in Fluent.
Source§

fn try_lookup(&self, lang: &LanguageIdentifier, text_id: &str) -> Option<String>

Look up 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>

Look up text_id for lang with args in Fluent.

Auto Trait Implementations§

§

impl<T> Freeze for MultiLoader<T>

§

impl<T> RefUnwindSafe for MultiLoader<T>
where T: RefUnwindSafe,

§

impl<T> Send for MultiLoader<T>
where T: Send,

§

impl<T> Sync for MultiLoader<T>
where T: Sync,

§

impl<T> Unpin for MultiLoader<T>
where T: Unpin,

§

impl<T> UnwindSafe for MultiLoader<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.