longbridgeapp / rust-i18n

A better and simply I18n crate for Rust.
MIT License
297 stars 32 forks source link

Cannot find function `_rust_i18n_translate` in the crate root #56

Closed cliftontoaster-reid closed 5 months ago

cliftontoaster-reid commented 1 year ago

Good morning, I was wondering if I did something wrong, which I'm sure of it. But sadly, I wasn't able to put the finger on what is wrong. In my main.rs I put

#[macro_use]
extern crate rust_i18n;

and in main()

#[tokio::main]
async fn main() {
  // lots of config checking 

  i18n!("locales", fallback = "en");

  // and more config
  }

but I keep getting

image

If this can help, this is my src tree. Just in case the file organization was the problem? I don't know, so I put it here.

image

I hope that someone smarter than me (easy due to only my 2 brain cells) can help me.

Thank you very much for your time.

huacnlee commented 1 year ago

Put i18n!("locales", fallback = "en"); outside of the main fn.

PatrickMcLennan commented 11 months ago

Hey there, I'm facing the same issue that isn't resolved by the above comment.

Code (src/home_view.rs):

use wasm_bindgen::prelude::*;
use yew::prelude::*;
use rust_i18n::t;

use components::Header::Header;

rust_i18n::i18n!();

#[function_component(Home)]
pub fn home_view() -> Html {
    html! {
        <>
            <Header />
            <div class="container row">
                <div class="col-6 offset-3">
                    <h1>{t!("h1")}</h1>
                </div>
            </div>
        </>
    }
}

#[wasm_bindgen]
pub fn run_home_view() -> Result<(), JsValue> {
    yew::Renderer::<Home>::new().hydrate();
    Ok(())
}

Cargo.toml

[package]
name = "home_view"
version = "0.0.1"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]
path = "./src/lib.rs"

[dependencies]
components = { path = "./../components" }
rust-i18n = "2.2.1"
wasm-bindgen = "0.2"
yew = { version = "0.21.0", features = ["hydration", "csr", "ssr"] }

Folder structure

|- locales
|   en.yml
|- src
|   home_view.rs
|   lib.rs
| Cargo.toml

Error:

error[E0425]: cannot find function `_rust_i18n_translate` in the crate root
  --> views/home_view/./src/home_view.rs:19:11
   |
19 |                     <h1>{t!("h1")}</h1>
   |                          ^^^^^^^^ not found in the crate root
   |
   = note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

I'm wondering if this a WASM constraint? In that maybe rust-i18n doesn't work on WASM? Would appreciate any guidance!

EDIT: I can get this to compile by removing the following LOC from src/home_view.rs

use rust_i18n::t;
// ...
rust_i18n::i18n!();

and instead implementing the following code in src/lib/rs

#[macro_use]
extern crate rust_i18n;

i18n!();
// ...

That compiles, but does not work, the page simply shows the untranslated en.h1 key and has an error in the browser console re: being unable to load some JS. I'm now pretty convinced this package does not work with WASM compile targets, but am hoping to be proven wrong!

j-mendez commented 11 months ago

Hey there, I'm facing the same issue that isn't resolved by the above comment.

Code (src/home_view.rs):

use wasm_bindgen::prelude::*;
use yew::prelude::*;
use rust_i18n::t;

use components::Header::Header;

rust_i18n::i18n!();

#[function_component(Home)]
pub fn home_view() -> Html {
    html! {
      <>
          <Header />
          <div class="container row">
              <div class="col-6 offset-3">
                  <h1>{t!("h1")}</h1>
              </div>
          </div>
      </>
    }
}

#[wasm_bindgen]
pub fn run_home_view() -> Result<(), JsValue> {
    yew::Renderer::<Home>::new().hydrate();
    Ok(())
}

Cargo.toml

[package]
name = "home_view"
version = "0.0.1"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]
path = "./src/lib.rs"

[dependencies]
components = { path = "./../components" }
rust-i18n = "2.2.1"
wasm-bindgen = "0.2"
yew = { version = "0.21.0", features = ["hydration", "csr", "ssr"] }

Folder structure

|- locales
|   en.yml
|- src
|   home_view.rs
|   lib.rs
| Cargo.toml

Error:

error[E0425]: cannot find function `_rust_i18n_translate` in the crate root
  --> views/home_view/./src/home_view.rs:19:11
   |
19 |                     <h1>{t!("h1")}</h1>
   |                          ^^^^^^^^ not found in the crate root
   |
   = note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

I'm wondering if this a WASM constraint? In that maybe rust-i18n doesn't work on WASM? Would appreciate any guidance!

EDIT: I can get this to compile by removing the following LOC from src/home_view.rs

use rust_i18n::t;
// ...
rust_i18n::i18n!();

and instead implementing the following code in src/lib/rs

#[macro_use]
extern crate rust_i18n;

i18n!();
// ...

That compiles, but does not work, the page simply shows the untranslated en.h1 key and has an error in the browser console re: being unable to load some JS. I'm now pretty convinced this package does not work with WASM compile targets, but am hoping to be proven wrong!

@PatrickMcLennan I experienced the same issue. If the crate has the translations statically stubbed out with &'static str you will get the translations into the binaries just fine. If you have dynamic translations it looks like you need to include the locales as well into the files on your Cargo.toml.

Example below shows how you can ship to wasm. I have two crates one pure Rust https://github.com/a11ywatch/accessibility-rs and another taking the Rust crate and binding the glue with wasm-bindgen https://github.com/a11ywatch/kayle/blob/main/kayle_innate/kayle_innate/src/lib.rs#L36. Run the command yarn test:puppeteer:innate in the kayle project to see the output of the test with translations.

ta3pks commented 6 months ago

any solution to this ?

franz65 commented 5 months ago

I have the same issue. It's an unpredictable behaviour, becasue the crate works fine inside one project and gives me the error cannot find function _rust_i18n_translate in the crate root inside another one. Can't figure out why.

j-mendez commented 5 months ago

I have the same issue. It's an unpredictable behaviour, becasue the crate works fine inside one project and gives me the error cannot find function _rust_i18n_translate in the crate root inside another one.

Can't figure out why.

Include the locales in the cargo toml.

franz65 commented 5 months ago

Doesn't work for me. I have it included inside [package] ... include = ["locales"]

I tried /locales, ./locales, nothing works. From main.rs it works without issues, I get the right text translated. It seems as it can't expand the macro correctly.

j-mendez commented 5 months ago

Doesn't work for me. I have it included inside [package] ... include = ["locales"]

I tried /locales, ./locales, nothing works. From main.rs it works without issues, I get the right text translated. It seems as it can't expand the macro correctly.

What version? I’m using v2 here and able to export https://github.com/a11ywatch/accessibility-rs.

franz65 commented 5 months ago

Version 3

franz65 commented 5 months ago

I tried with version 2, but the problem remains. Thanks anyway

j-mendez commented 5 months ago

I tried with version 2, but the problem remains.

Thanks anyway

What version of rust? I think I had the same issue before, not sure what fixed it.

franz65 commented 5 months ago

rustc version is `rustc 1.77.1

franz65 commented 5 months ago

As suggested by @huacnlee in another thread, putting


i18n!();```
inside lib.rs at root level solves the problem.
huacnlee commented 5 months ago

Make sure the i18n! is in the main.rs or lib.rs root.

image