XAMPPRocky / fluent-templates

Easily add Fluent to your Rust project.
Apache License 2.0
136 stars 28 forks source link

Example does not work #29

Closed makorne closed 3 years ago

makorne commented 3 years ago

Hi something went wrong. rustc 1.52.1

use std::collections::HashMap;

use unic_langid::{LanguageIdentifier, langid};
use fluent_templates::{Loader, static_loader};

const US_ENGLISH: LanguageIdentifier = langid!("en-US");
const FRENCH: LanguageIdentifier = langid!("fr");
const GERMAN: LanguageIdentifier = langid!("de");

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() {
    assert_eq!("Hello World!", LOCALES.lookup(&US_ENGLISH, "hello-world"));
    assert_eq!("Bonjour le monde!", LOCALES.lookup(&FRENCH, "hello-world"));
    assert_eq!("Hallo Welt!", LOCALES.lookup(&GERMAN, "hello-world"));

    let args = {
        let mut map = HashMap::new();
        map.insert(String::from("name"), "Alice".into());
        map
    };

    assert_eq!("Hello Alice!", LOCALES.lookup_with_args(&US_ENGLISH, "greeting", &args));
    assert_eq!("Bonjour Alice!", LOCALES.lookup_with_args(&FRENCH, "greeting", &args));
    assert_eq!("Hallo Alice!", LOCALES.lookup_with_args(&GERMAN, "greeting", &args));
}
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Fluent { source: FluentError([ParserError { pos: (0, 1), slice: Some((0, 64)), kind: ExpectedCharRange { range: "a-zA-Z" } }]) }', src/main.rs:10:1
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/core/src/panicking.rs:92:14
   2: core::option::expect_none_failed
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/core/src/option.rs:1329:5
   3: core::result::Result<T,E>::unwrap
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1037:23
   4: Test::LOCALES::{{closure}}::RESOURCES::{{closure}}
             at ./src/main.rs:10:1
   5: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
   6: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
   7: once_cell::sync::Lazy<T,F>::force::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1023:28
   8: once_cell::sync::OnceCell<T>::get_or_init::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:845:57
   9: once_cell::imp::OnceCell<T>::initialize::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:95:19
  10: once_cell::imp::initialize_inner
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:171:31
  11: once_cell::imp::OnceCell<T>::initialize
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:93:9
  12: once_cell::sync::OnceCell<T>::get_or_try_init
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:885:13
  13: once_cell::sync::OnceCell<T>::get_or_init
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:845:19
  14: once_cell::sync::Lazy<T,F>::force
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1022:13
  15: <once_cell::sync::Lazy<T,F> as core::ops::deref::Deref>::deref
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1032:13
  16: Test::LOCALES::{{closure}}::BUNDLES::{{closure}}
             at ./src/main.rs:10:1
  17: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
  18: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
  19: once_cell::sync::Lazy<T,F>::force::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1023:28
  20: once_cell::sync::OnceCell<T>::get_or_init::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:845:57
  21: once_cell::imp::OnceCell<T>::initialize::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:95:19
  22: once_cell::imp::initialize_inner
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:171:31
  23: once_cell::imp::OnceCell<T>::initialize
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:93:9
  24: once_cell::sync::OnceCell<T>::get_or_try_init
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:885:13
  25: once_cell::sync::OnceCell<T>::get_or_init
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:845:19
  26: once_cell::sync::Lazy<T,F>::force
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1022:13
  27: <once_cell::sync::Lazy<T,F> as core::ops::deref::Deref>::deref
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1032:13
  28: Test::LOCALES::{{closure}}
             at ./src/main.rs:10:1
  29: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
  30: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
  31: once_cell::sync::Lazy<T,F>::force::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1023:28
  32: once_cell::sync::OnceCell<T>::get_or_init::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:845:57
  33: once_cell::imp::OnceCell<T>::initialize::{{closure}}
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:95:19
  34: once_cell::imp::initialize_inner
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:171:31
  35: once_cell::imp::OnceCell<T>::initialize
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/imp_std.rs:93:9
  36: once_cell::sync::OnceCell<T>::get_or_try_init
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:885:13
  37: once_cell::sync::OnceCell<T>::get_or_init
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:845:19
  38: once_cell::sync::Lazy<T,F>::force
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1022:13
  39: <once_cell::sync::Lazy<T,F> as core::ops::deref::Deref>::deref
             at /home/f/.cargo/registry/src/github.com-1ecc6299db9ec823/once_cell-1.7.2/src/lib.rs:1032:13
  40: Test::main
             at ./src/main.rs:21:32
  41: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
XAMPPRocky commented 3 years ago

Thank you for your issue! It looks like there's an error in your fluent source code. Specifically a-zA-Z is invalid.

MichaelBradetLegris commented 2 years ago

I'm also running into this issue. The provided answer is a little unclear. Could you please specify @XAMPPRocky? What part of the code precisely is wrong?

XAMPPRocky commented 2 years ago

@MichaelBradetLegris Your fluent (.ftl) code has invalid syntax, I couldn't tell you more without seeing it.

TimMaher commented 1 year ago

Closed or not, I have just encountered this problem and solved it by re-naming all of the equivalent variables in each different ftl file. Which makes me wonder why there are different locale directories in the first place.

XAMPPRocky commented 1 year ago

@TimMaher Each file in a locale directory is bundled into a single FluentBundle for the locale. That's not how variables work so you were running into a different issue, which happened to be fixed when you renamed your variables. I'm not sure you're even talking about variables, as you can't define variables, you can only use variables in a placeable of a term, and like what I said above, terms are not shared between locales. If you want to have shared terms use the core_locales in the static loader, or the shared_resources option.

If you're getting the error above, your fluent syntax was incorrect.

TimMaher commented 1 year ago

Thanks XAMPP,

We are talking about slightly different things I guess, largely because my language was imprecise. The “variables” I was referring to are items in each of the .ftl files. So, if I have an item with a label hello and that item is in each of locales/core.ftl and locales/en/main.ftl it will throw the error when I try to get the value in my main function. I have to give them different names in each file. In this instance I have called the first one hello and the second one en-hello.

Perhaps I have not fully understood the static loader implementation?

Best Regs

TIm Tim Maher-De Troyer @.***

BTW… Happy New Year! Bit late but...

On 8 Jan 2023, at 17:36, XAMPPRocky @.***> wrote:

@TimMaher https://github.com/TimMaher Each file in a locale directory is bundled into a single FluentBundle for the locale. That's not how variables work so you were running into a different issue, which happened to be fixed when you renamed your variables. I'm not sure you're even talking about variables, as you can't define variables, you can only use variables in a placeable of a term, and like what I said above, terms are not shared between locales. If you want to have shared terms use the core_locales https://docs.rs/fluent-templates/latest/fluent_templates/macro.static_loader.html in the static loader, or the shared_resources https://docs.rs/fluent-templates/latest/fluent_templates/struct.ArcLoaderBuilder.html#method.shared_resources option.

If you're getting the error above, your fluent syntax was incorrect.

— Reply to this email directly, view it on GitHub https://github.com/XAMPPRocky/fluent-templates/issues/29#issuecomment-1374888155, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAVYV5P4T2XLVIK5HH574NDWRL3JNANCNFSM45YFA47Q. You are receiving this because you were mentioned.

XAMPPRocky commented 1 year ago

So, if I have an item with a label hello and that item is in each of locales/core.ftl and locales/en/main.ftl it will throw the error when I try to get the value in my main function.

Yes, you shouldn't define them in both, that's equivalent to defining it twice in a single file, which is the error you're getting. You shouldn't be trying to overriding it with the same message ID. Core locales are for things that are always the same across all locales, and you should have separate messages or terms defined when its locale specific.

Without knowing the specifics you shouldn't have hello and en-hello, as that's not actually descriptive or cross-locale compatible. You should have something like hello and hello-message.

TimMaher commented 1 year ago

Ahh... that explains it. It’s in the core. I should have spotted that as I had to specify a default, which would not, therefore, expect a core.

Thanks for that.

Regs

Tim

Sent from my iPad

On 8 Jan 2023, at 18:16, XAMPPRocky @.***> wrote:

So, if I have an item with a label hello and that item is in each of locales/core.ftl and locales/en/main.ftl it will throw the error when I try to get the value in my main function.

Yes, you shouldn't define them in both, that's equivalent to defining it twice in a single file, which is the error you're getting. You shouldn't be trying to overriding it with the same message ID. Core locales are for things that are always the same across all locales, and you should have separate messages or terms defined when its locale specific.

Without knowing the specifics you shouldn't have hello and en-hello, as that's not actually descriptive or cross-locale compatible. You should have something like hello and hello-message.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.