Skip to content

Commit

Permalink
Update example for share I18n in entire workspace (#80)
Browse files Browse the repository at this point in the history
Based on real world uses: huacnlee/zed#3

Update example to embed I18n in single crate, and share it in entire
workspace.
  • Loading branch information
huacnlee authored Jun 3, 2024
1 parent 33f5fca commit ce793cc
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/app-egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "../.." }
Expand Down
12 changes: 12 additions & 0 deletions examples/share-in-workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
13 changes: 13 additions & 0 deletions examples/share-in-workspace/README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "my-i18n"
name = "i18n"
version = "0.1.0"

[dependencies]
rust-i18n = { path = "../../.." }
rust-i18n.workspace = true
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_i18n::Backend;

rust_i18n::i18n!("../locales");
rust_i18n::i18n!("../../locales");

pub struct I18nBackend;

Expand All @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions examples/share-in-workspace/crates/my-app1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rust_i18n::i18n!(backend = my_i18n::I18nBackend);
i18n::init!();

#[cfg(test)]
mod tests {
Expand Down
File renamed without changes.
11 changes: 0 additions & 11 deletions examples/share-locales-in-workspace/Cargo.toml

This file was deleted.

18 changes: 0 additions & 18 deletions examples/share-locales-in-workspace/my-app1/src/lib.rs

This file was deleted.

0 comments on commit ce793cc

Please sign in to comment.