diff --git a/.gitignore b/.gitignore index 021ccd6..9926b4f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target Cargo.lock debug.txt .idea/ +.DS_Store diff --git a/Makefile b/Makefile index 73db3b8..d1ca8bd 100644 --- a/Makefile +++ b/Makefile @@ -9,3 +9,4 @@ release: test: cargo test --workspace cargo test --manifest-path examples/app-workspace/Cargo.toml --workspace + cargo test --manifest-path examples/share-locales-in-workspace/Cargo.toml --workspace diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index 83e7f98..8d1d076 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -154,6 +154,8 @@ fn generate_code( use rust_i18n::BackendExt; /// I18n backend instance + /// + /// [PUBLIC] This is a public API, and as an example in examples/ static _RUST_I18N_BACKEND: rust_i18n::once_cell::sync::Lazy> = rust_i18n::once_cell::sync::Lazy::new(|| { let mut backend = rust_i18n::SimpleBackend::new(); #(#all_translations)* diff --git a/examples/share-locales-in-workspace/Cargo.toml b/examples/share-locales-in-workspace/Cargo.toml new file mode 100644 index 0000000..0374791 --- /dev/null +++ b/examples/share-locales-in-workspace/Cargo.toml @@ -0,0 +1,11 @@ +name = "sare-locales-in-workspace" + +[workspace] +members = [ + "my-app1", + "my-app2", + "my-i18n" +] + +[workspace.dependencies] +my-i18n = { path = "./my-i18n" } diff --git a/examples/share-locales-in-workspace/locales/app.yml b/examples/share-locales-in-workspace/locales/app.yml new file mode 100644 index 0000000..31973a1 --- /dev/null +++ b/examples/share-locales-in-workspace/locales/app.yml @@ -0,0 +1,5 @@ +_version: 2 +welcome: + en: Rust I18n Example for share locales in entire workspace. + zh-CN: Rust I18n 示例,用于在整个工作区中共享本地化。 + zh-HK: Rust I18n 示例,用於在整個工作區中共享本地化。 diff --git a/examples/share-locales-in-workspace/my-app1/Cargo.toml b/examples/share-locales-in-workspace/my-app1/Cargo.toml new file mode 100644 index 0000000..422a659 --- /dev/null +++ b/examples/share-locales-in-workspace/my-app1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +edition = "2021" +name = "my-app1" +version = "0.1.0" + +[dependencies] +rust-i18n = { path = "../../../../rust-i18n" } +my-i18n.workspace = true diff --git a/examples/share-locales-in-workspace/my-app1/src/lib.rs b/examples/share-locales-in-workspace/my-app1/src/lib.rs new file mode 100644 index 0000000..4f36165 --- /dev/null +++ b/examples/share-locales-in-workspace/my-app1/src/lib.rs @@ -0,0 +1,18 @@ +rust_i18n::i18n!(backend = my_i18n::I18nBackend); + +#[cfg(test)] +mod tests { + use rust_i18n::t; + + #[test] + fn test_load_str() { + assert_eq!( + t!("welcome", locale = "en"), + "Rust I18n Example for share locales in entire workspace." + ); + assert_eq!( + t!("welcome", locale = "zh-CN"), + "Rust I18n 示例,用于在整个工作区中共享本地化。" + ); + } +} diff --git a/examples/share-locales-in-workspace/my-app2/Cargo.toml b/examples/share-locales-in-workspace/my-app2/Cargo.toml new file mode 100644 index 0000000..f833d96 --- /dev/null +++ b/examples/share-locales-in-workspace/my-app2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +edition = "2021" +name = "my-app2" +version = "0.1.0" + +[dependencies] +rust-i18n = { path = "../../../../rust-i18n" } +my-i18n.workspace = true diff --git a/examples/share-locales-in-workspace/my-app2/src/lib.rs b/examples/share-locales-in-workspace/my-app2/src/lib.rs new file mode 100644 index 0000000..4f36165 --- /dev/null +++ b/examples/share-locales-in-workspace/my-app2/src/lib.rs @@ -0,0 +1,18 @@ +rust_i18n::i18n!(backend = my_i18n::I18nBackend); + +#[cfg(test)] +mod tests { + use rust_i18n::t; + + #[test] + fn test_load_str() { + assert_eq!( + t!("welcome", locale = "en"), + "Rust I18n Example for share locales in entire workspace." + ); + assert_eq!( + t!("welcome", locale = "zh-CN"), + "Rust I18n 示例,用于在整个工作区中共享本地化。" + ); + } +} diff --git a/examples/share-locales-in-workspace/my-i18n/Cargo.toml b/examples/share-locales-in-workspace/my-i18n/Cargo.toml new file mode 100644 index 0000000..c332a13 --- /dev/null +++ b/examples/share-locales-in-workspace/my-i18n/Cargo.toml @@ -0,0 +1,7 @@ +[package] +edition = "2021" +name = "my-i18n" +version = "0.1.0" + +[dependencies] +rust-i18n = { path = "../../../../rust-i18n" } diff --git a/examples/share-locales-in-workspace/my-i18n/src/lib.rs b/examples/share-locales-in-workspace/my-i18n/src/lib.rs new file mode 100644 index 0000000..1cf6361 --- /dev/null +++ b/examples/share-locales-in-workspace/my-i18n/src/lib.rs @@ -0,0 +1,20 @@ +use rust_i18n::Backend; + +rust_i18n::i18n!("../locales"); + +pub struct I18nBackend; + +impl Backend for I18nBackend { + fn available_locales(&self) -> Vec<&str> { + _RUST_I18N_BACKEND.available_locales() + } + + fn translate(&self, locale: &str, key: &str) -> Option<&str> { + let val = _RUST_I18N_BACKEND.translate(locale, key); + if val.is_none() { + _RUST_I18N_BACKEND.translate("en", key) + } else { + val + } + } +}