longbridgeapp / rust-i18n

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

fix lazy init always "en" #88

Closed yk0n9 closed 3 weeks ago

yk0n9 commented 3 weeks ago

close #87

KKRainbow commented 3 weeks ago

Could you please add me as a co-author?

yk0n9 commented 3 weeks ago

Could you please add me as a co-author?

of course

huacnlee commented 3 weeks ago

Hi @yk0n9 I have make a new change to do this.

https://github.com/longbridgeapp/rust-i18n/pull/90

yk0n9 commented 3 weeks ago

Hi @yk0n9 I have make a new change to do this.

90

I feel like your code doesn't fix it😂

  1. _RUST_I18N_BACKEND is lazy, set_locale does not trigger _RUST_I18N_BACKEND initialization, t!() will trigger _RUST_I18N_BACKEND initialization.
set_locale("zh");
t!("xxx"); // <-- backend is initialized here
  1. The initialization process only has the default value, and will not get the value from CURRENT_LOCALE. This will make the backend always "en" and will overwrite the first line of set_locale("zh");.

  2. We must use locale() to get CURRENT_LOCALE while initializing _RUST_I18N_BACKEND. It ensures that the first line set_locale("zh"); will not be overwritten.

yk0n9 commented 3 weeks ago
    #[test]
    fn test_set_locale() {
        rust_i18n::set_locale("zh-CN");
        for _ in 0..5 {
            assert_eq!(t!("hello"), "Bar - 你好世界!");
        }
    }
---- tests::test_set_locale stdout ----
thread 'tests::test_set_locale' panicked at tests\integration_tests.rs:323:13:
assertion `left == right` failed
  left: "Bar - Hello, World!"
 right: "Bar - 你好世界!"
stack backtrace:
   0: rust_begin_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library\std\src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library\core\src/panicking.rs:72:14
   2: core::panicking::assert_failed_inner
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library\core\src/panicking.rs:408:17
   3: core::panicking::assert_failed
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23\library\core\src/panicking.rs:363:5
   4: integration_tests::tests::test_set_locale
             at .\tests\integration_tests.rs:323:13
   5: integration_tests::tests::test_set_locale::{{closure}}
             at .\tests\integration_tests.rs:320:25
   6: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23\library\core\src\ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library\core\src\ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

failures:
    tests::test_set_locale

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 18 filtered out; finished in 0.01s

error: test failed, to rerun pass `-p rust-i18n --test integration_tests`

Apparently, the test has failed in the merged code

huacnlee commented 3 weeks ago

Oops, I remember I passed the test. 😂

huacnlee commented 3 weeks ago

Please merge main into your branch, I am going to merge this.