From 4eabf5cf28d67325752e415fe7f45239c79f8206 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sat, 1 Jun 2024 23:54:53 +0800 Subject: [PATCH 1/4] Add example for share I18n in entire workspace. --- examples/share-i18n/crates/app/src/main.rs | 0 examples/share-i18n/crates/i18n/src/lib.rs | 15 +++++++++++++++ examples/share-i18n/locales/i18n.yml | 4 ++++ 3 files changed, 19 insertions(+) create mode 100644 examples/share-i18n/crates/app/src/main.rs create mode 100644 examples/share-i18n/crates/i18n/src/lib.rs create mode 100644 examples/share-i18n/locales/i18n.yml diff --git a/examples/share-i18n/crates/app/src/main.rs b/examples/share-i18n/crates/app/src/main.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/share-i18n/crates/i18n/src/lib.rs b/examples/share-i18n/crates/i18n/src/lib.rs new file mode 100644 index 0000000..155ad4f --- /dev/null +++ b/examples/share-i18n/crates/i18n/src/lib.rs @@ -0,0 +1,15 @@ +use rust_i18n; + +rust_i18n::i18n!("locales", fallback = "en"); + +pub struct Backend; + +impl rust_i18n::Backend for Backend { + fn available_locales(&self) -> Vec<&str> { + _RUST_I18N_BACKEND.available_locales() + } + + fn translate<'a>(&'a self, locale: &str, key: &str) -> Option<&str> { + _RUST_I18N_BACKEND.translate(locale, key) + } +} diff --git a/examples/share-i18n/locales/i18n.yml b/examples/share-i18n/locales/i18n.yml new file mode 100644 index 0000000..ebd3f2b --- /dev/null +++ b/examples/share-i18n/locales/i18n.yml @@ -0,0 +1,4 @@ +_version: 2 +hello: + en: Hello world. + zh-CN: Hello 世界。 \ No newline at end of file From 5139b928d66d8285a864503a1bf5c91180b1f98c Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 3 Jun 2024 09:50:31 +0800 Subject: [PATCH 2/4] Update example --- examples/share-i18n/crates/app/src/main.rs | 0 examples/share-i18n/crates/i18n/src/lib.rs | 15 ------------ examples/share-i18n/locales/i18n.yml | 4 ---- examples/share-in-workspace/Cargo.toml | 12 ++++++++++ examples/share-in-workspace/README.md | 13 +++++++++++ .../crates/i18n}/Cargo.toml | 4 ++-- .../crates/i18n}/src/lib.rs | 12 +++++++++- .../crates}/my-app1/Cargo.toml | 4 ++-- .../crates/my-app1/src/lib.rs | 23 +++++++++++++++++++ .../crates}/my-app2/Cargo.toml | 4 ++-- .../crates/my-app2}/src/lib.rs | 2 +- .../locales/app.yml | 0 .../share-locales-in-workspace/Cargo.toml | 11 --------- .../my-app2/src/lib.rs | 18 --------------- 14 files changed, 66 insertions(+), 56 deletions(-) delete mode 100644 examples/share-i18n/crates/app/src/main.rs delete mode 100644 examples/share-i18n/crates/i18n/src/lib.rs delete mode 100644 examples/share-i18n/locales/i18n.yml create mode 100644 examples/share-in-workspace/Cargo.toml create mode 100644 examples/share-in-workspace/README.md rename examples/{share-locales-in-workspace/my-i18n => share-in-workspace/crates/i18n}/Cargo.toml (54%) rename examples/{share-locales-in-workspace/my-i18n => share-in-workspace/crates/i18n}/src/lib.rs (68%) rename examples/{share-locales-in-workspace => share-in-workspace/crates}/my-app1/Cargo.toml (56%) create mode 100644 examples/share-in-workspace/crates/my-app1/src/lib.rs rename examples/{share-locales-in-workspace => share-in-workspace/crates}/my-app2/Cargo.toml (56%) rename examples/{share-locales-in-workspace/my-app1 => share-in-workspace/crates/my-app2}/src/lib.rs (88%) rename examples/{share-locales-in-workspace => share-in-workspace}/locales/app.yml (100%) delete mode 100644 examples/share-locales-in-workspace/Cargo.toml delete mode 100644 examples/share-locales-in-workspace/my-app2/src/lib.rs diff --git a/examples/share-i18n/crates/app/src/main.rs b/examples/share-i18n/crates/app/src/main.rs deleted file mode 100644 index e69de29..0000000 diff --git a/examples/share-i18n/crates/i18n/src/lib.rs b/examples/share-i18n/crates/i18n/src/lib.rs deleted file mode 100644 index 155ad4f..0000000 --- a/examples/share-i18n/crates/i18n/src/lib.rs +++ /dev/null @@ -1,15 +0,0 @@ -use rust_i18n; - -rust_i18n::i18n!("locales", fallback = "en"); - -pub struct Backend; - -impl rust_i18n::Backend for Backend { - fn available_locales(&self) -> Vec<&str> { - _RUST_I18N_BACKEND.available_locales() - } - - fn translate<'a>(&'a self, locale: &str, key: &str) -> Option<&str> { - _RUST_I18N_BACKEND.translate(locale, key) - } -} diff --git a/examples/share-i18n/locales/i18n.yml b/examples/share-i18n/locales/i18n.yml deleted file mode 100644 index ebd3f2b..0000000 --- a/examples/share-i18n/locales/i18n.yml +++ /dev/null @@ -1,4 +0,0 @@ -_version: 2 -hello: - en: Hello world. - zh-CN: Hello 世界。 \ No newline at end of file diff --git a/examples/share-in-workspace/Cargo.toml b/examples/share-in-workspace/Cargo.toml new file mode 100644 index 0000000..4e7e1f1 --- /dev/null +++ b/examples/share-in-workspace/Cargo.toml @@ -0,0 +1,12 @@ +name = "sare-locales-in-workspace" + +[workspace] +members = [ + "crates/my-app1", + "crates/my-app2", + "crates/i18n" +] + +[workspace.dependencies] +rust-i18n = { path = "../../" } +i18n = { path = "crates/i18n" } diff --git a/examples/share-in-workspace/README.md b/examples/share-in-workspace/README.md new file mode 100644 index 0000000..5e14272 --- /dev/null +++ b/examples/share-in-workspace/README.md @@ -0,0 +1,13 @@ +# An example of share I18n translations between crates. + +This example is shows how to share I18n translations between crates. We only embed the translations into `i18n` crate once, and then share them with other crates. + +Unlike use `rust_i18n::i18n!` macro to load translations in each crate, we just only load translations in `i18n` crate, it will save memory and reduce the size of the final binary. + +本示例展示了如何在不同的 crate 之间共享 I18n 翻译。我们只需要在 `i18n` crate 中嵌入翻译一次,然后就可以在其他 crate 中共享它们。 + +与在每个 crate 中使用 `rust_i18n::i18n!` 宏加载翻译不同,我们只需要在 `i18n` crate 中加载翻译,这样可以节省内存并减小最终二进制文件的大小。 + +- [i18n](crate/i18n) - Used to load and share translations, and provide a macro to get translations. +- [my-app1](crate/my-app1) - An example of using `i18n` crate to get translations. +- [my-app2](crate/my-app2) - Another example of using `i18n` crate to get translations. diff --git a/examples/share-locales-in-workspace/my-i18n/Cargo.toml b/examples/share-in-workspace/crates/i18n/Cargo.toml similarity index 54% rename from examples/share-locales-in-workspace/my-i18n/Cargo.toml rename to examples/share-in-workspace/crates/i18n/Cargo.toml index e3dfecf..5aad2f0 100644 --- a/examples/share-locales-in-workspace/my-i18n/Cargo.toml +++ b/examples/share-in-workspace/crates/i18n/Cargo.toml @@ -1,7 +1,7 @@ [package] edition = "2021" -name = "my-i18n" +name = "i18n" version = "0.1.0" [dependencies] -rust-i18n = { path = "../../.." } +rust-i18n.workspace = true diff --git a/examples/share-locales-in-workspace/my-i18n/src/lib.rs b/examples/share-in-workspace/crates/i18n/src/lib.rs similarity index 68% rename from examples/share-locales-in-workspace/my-i18n/src/lib.rs rename to examples/share-in-workspace/crates/i18n/src/lib.rs index 1cf6361..68f878e 100644 --- a/examples/share-locales-in-workspace/my-i18n/src/lib.rs +++ b/examples/share-in-workspace/crates/i18n/src/lib.rs @@ -1,6 +1,6 @@ use rust_i18n::Backend; -rust_i18n::i18n!("../locales"); +rust_i18n::i18n!("../../locales"); pub struct I18nBackend; @@ -18,3 +18,13 @@ impl Backend for I18nBackend { } } } + +#[macro_export] +macro_rules! init { + () => { + rust_i18n::i18n!(backend = i18n::I18nBackend); + }; +} + +pub use rust_i18n::set_locale; +pub use rust_i18n::t; diff --git a/examples/share-locales-in-workspace/my-app1/Cargo.toml b/examples/share-in-workspace/crates/my-app1/Cargo.toml similarity index 56% rename from examples/share-locales-in-workspace/my-app1/Cargo.toml rename to examples/share-in-workspace/crates/my-app1/Cargo.toml index 5edff0c..114a1b1 100644 --- a/examples/share-locales-in-workspace/my-app1/Cargo.toml +++ b/examples/share-in-workspace/crates/my-app1/Cargo.toml @@ -4,5 +4,5 @@ name = "my-app1" version = "0.1.0" [dependencies] -rust-i18n = { path = "../../.." } -my-i18n.workspace = true +rust-i18n.workspace = true +i18n.workspace = true diff --git a/examples/share-in-workspace/crates/my-app1/src/lib.rs b/examples/share-in-workspace/crates/my-app1/src/lib.rs new file mode 100644 index 0000000..0fad898 --- /dev/null +++ b/examples/share-in-workspace/crates/my-app1/src/lib.rs @@ -0,0 +1,23 @@ +use i18n::t; + +i18n::init!(); + +#[allow(dead_code)] +fn assert_messages() { + assert_eq!( + t!("welcome", locale = "en"), + "Rust I18n Example for share locales in entire workspace." + ); + assert_eq!( + t!("welcome", locale = "zh-CN"), + "Rust I18n 示例,用于在整个工作区中共享本地化。" + ); +} + +#[cfg(test)] +mod tests { + #[test] + fn test_load_str() { + super::assert_messages(); + } +} diff --git a/examples/share-locales-in-workspace/my-app2/Cargo.toml b/examples/share-in-workspace/crates/my-app2/Cargo.toml similarity index 56% rename from examples/share-locales-in-workspace/my-app2/Cargo.toml rename to examples/share-in-workspace/crates/my-app2/Cargo.toml index ec3fe00..cdb5692 100644 --- a/examples/share-locales-in-workspace/my-app2/Cargo.toml +++ b/examples/share-in-workspace/crates/my-app2/Cargo.toml @@ -4,5 +4,5 @@ name = "my-app2" version = "0.1.0" [dependencies] -rust-i18n = { path = "../../.." } -my-i18n.workspace = true +rust-i18n.workspace = true +i18n.workspace = true diff --git a/examples/share-locales-in-workspace/my-app1/src/lib.rs b/examples/share-in-workspace/crates/my-app2/src/lib.rs similarity index 88% rename from examples/share-locales-in-workspace/my-app1/src/lib.rs rename to examples/share-in-workspace/crates/my-app2/src/lib.rs index 4f36165..581f2a7 100644 --- a/examples/share-locales-in-workspace/my-app1/src/lib.rs +++ b/examples/share-in-workspace/crates/my-app2/src/lib.rs @@ -1,4 +1,4 @@ -rust_i18n::i18n!(backend = my_i18n::I18nBackend); +i18n::init!(); #[cfg(test)] mod tests { diff --git a/examples/share-locales-in-workspace/locales/app.yml b/examples/share-in-workspace/locales/app.yml similarity index 100% rename from examples/share-locales-in-workspace/locales/app.yml rename to examples/share-in-workspace/locales/app.yml diff --git a/examples/share-locales-in-workspace/Cargo.toml b/examples/share-locales-in-workspace/Cargo.toml deleted file mode 100644 index 0374791..0000000 --- a/examples/share-locales-in-workspace/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -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/my-app2/src/lib.rs b/examples/share-locales-in-workspace/my-app2/src/lib.rs deleted file mode 100644 index 4f36165..0000000 --- a/examples/share-locales-in-workspace/my-app2/src/lib.rs +++ /dev/null @@ -1,18 +0,0 @@ -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 示例,用于在整个工作区中共享本地化。" - ); - } -} From b8abc3d2a65536cee88ecdb0a8c9a7fd3f8d6a0c Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 3 Jun 2024 09:55:47 +0800 Subject: [PATCH 3/4] Fix test --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 29b283b..2dc1f55 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,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 + cargo test --manifest-path examples/share-in-workspace/Cargo.toml --workspace From a1d03e80154f4e8245acef58616b26020ddc87d2 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 3 Jun 2024 10:01:00 +0800 Subject: [PATCH 4/4] Upgrade eframe --- examples/app-egui/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/app-egui/Cargo.toml b/examples/app-egui/Cargo.toml index 7aca74d..e9a0135 100644 --- a/examples/app-egui/Cargo.toml +++ b/examples/app-egui/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -eframe = "0.25.0" +eframe = "0.27.2" env_logger = { version = "0.11", optional = true } log = { version = "0.4", optional = true } rust-i18n = { path = "../.." }