kellpossible / cargo-i18n

A Rust Cargo sub-command and libraries to extract and build localization resources to embed in your application/library
MIT License
121 stars 25 forks source link

No localization for ID in WASM context #95

Closed TimBoettcher closed 1 year ago

TimBoettcher commented 1 year ago

Consider the following example:

use i18n_embed::{WebLanguageRequester, fluent::{fluent_language_loader}, LanguageLoader};
use rust_embed::RustEmbed;
use unic_langid::LanguageIdentifier;
use yew::prelude::*;

#[derive(RustEmbed)]
#[folder = "i18n"]
struct Localizations;

#[function_component(App)]
fn app() -> Html {
    let language_loader = fluent_language_loader!();
    let requested_languages = WebLanguageRequester::requested_languages();
    let _result = i18n_embed::select(&language_loader, &Localizations, &requested_languages);

    html! {
        <main>
            <p>{ language_loader.get_lang(&[&"de-DE".parse::<LanguageIdentifier>().unwrap()], "hello") }</p>
            <p>{ language_loader.get("hello") }</p>
        </main>
    }
}

fn main() {
    yew::start_app::<App>();
}

I use trunk to build and bundle this into a WASM application, but unfortunately I get the following message in both above cases:

No localization for id: "hello"

I converted this to a native application and confirmed that it worked as it should (displaying Hallo, Welt!) in the desktop context, but I'm at a loss what could possibly be causing this to fail on the WASM target.

Do you have any ideas?

TimBoettcher commented 1 year ago

I found the solution, which has to do with rust-embed:

rust-embed = { version = "6", features = [ "debug-embed" ] }

You might want to add this to the documentation; in any case, i18n-embed works even in a WASM context.

kellpossible commented 1 year ago

@TimBoettcher I'd like to add a WASM example, would be happy for contribution :slightly_smiling_face: