Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix lazy init always "en" #88

Merged
merged 3 commits into from
Aug 20, 2024
Merged

fix lazy init always "en" #88

merged 3 commits into from
Aug 20, 2024

Conversation

yk0n9
Copy link
Contributor

@yk0n9 yk0n9 commented Aug 20, 2024

close #87

@KKRainbow
Copy link
Contributor

Could you please add me as a co-author?

Co-authored-by: yk0n9 <[email protected]>
Co-authored-by: KKRainbow <[email protected]>
@yk0n9
Copy link
Contributor Author

yk0n9 commented Aug 20, 2024

Could you please add me as a co-author?

of course

huacnlee added a commit that referenced this pull request Aug 20, 2024
Closes #87 #88

Co-authored-by: yk0n9 <[email protected]>
Co-authored-by: KKRainbow <[email protected]>
@huacnlee
Copy link
Member

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

#90

@huacnlee huacnlee closed this Aug 20, 2024
@yk0n9
Copy link
Contributor Author

yk0n9 commented Aug 20, 2024

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
Copy link
Contributor Author

yk0n9 commented Aug 20, 2024

    #[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
Copy link
Member

Oops, I remember I passed the test. 😂

@huacnlee huacnlee reopened this Aug 20, 2024
@huacnlee
Copy link
Member

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

@huacnlee huacnlee merged commit 52033e7 into longbridge:main Aug 20, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Now only the first time is i18n
3 participants