Skip to content

Commit ce793cc

Browse files
authored
Update example for share I18n in entire workspace (#80)
Based on real world uses: huacnlee/zed#3 Update example to embed I18n in single crate, and share it in entire workspace.
1 parent 33f5fca commit ce793cc

File tree

13 files changed

+68
-39
lines changed

13 files changed

+68
-39
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ release:
1111
test:
1212
cargo test --workspace
1313
cargo test --manifest-path examples/app-workspace/Cargo.toml --workspace
14-
cargo test --manifest-path examples/share-locales-in-workspace/Cargo.toml --workspace
14+
cargo test --manifest-path examples/share-in-workspace/Cargo.toml --workspace

examples/app-egui/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
eframe = "0.25.0"
9+
eframe = "0.27.2"
1010
env_logger = { version = "0.11", optional = true }
1111
log = { version = "0.4", optional = true }
1212
rust-i18n = { path = "../.." }
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name = "sare-locales-in-workspace"
2+
3+
[workspace]
4+
members = [
5+
"crates/my-app1",
6+
"crates/my-app2",
7+
"crates/i18n"
8+
]
9+
10+
[workspace.dependencies]
11+
rust-i18n = { path = "../../" }
12+
i18n = { path = "crates/i18n" }

examples/share-in-workspace/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# An example of share I18n translations between crates.
2+
3+
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.
4+
5+
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.
6+
7+
本示例展示了如何在不同的 crate 之间共享 I18n 翻译。我们只需要在 `i18n` crate 中嵌入翻译一次,然后就可以在其他 crate 中共享它们。
8+
9+
与在每个 crate 中使用 `rust_i18n::i18n!` 宏加载翻译不同,我们只需要在 `i18n` crate 中加载翻译,这样可以节省内存并减小最终二进制文件的大小。
10+
11+
- [i18n](crate/i18n) - Used to load and share translations, and provide a macro to get translations.
12+
- [my-app1](crate/my-app1) - An example of using `i18n` crate to get translations.
13+
- [my-app2](crate/my-app2) - Another example of using `i18n` crate to get translations.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
edition = "2021"
3-
name = "my-i18n"
3+
name = "i18n"
44
version = "0.1.0"
55

66
[dependencies]
7-
rust-i18n = { path = "../../.." }
7+
rust-i18n.workspace = true

examples/share-locales-in-workspace/my-i18n/src/lib.rs examples/share-in-workspace/crates/i18n/src/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rust_i18n::Backend;
22

3-
rust_i18n::i18n!("../locales");
3+
rust_i18n::i18n!("../../locales");
44

55
pub struct I18nBackend;
66

@@ -18,3 +18,13 @@ impl Backend for I18nBackend {
1818
}
1919
}
2020
}
21+
22+
#[macro_export]
23+
macro_rules! init {
24+
() => {
25+
rust_i18n::i18n!(backend = i18n::I18nBackend);
26+
};
27+
}
28+
29+
pub use rust_i18n::set_locale;
30+
pub use rust_i18n::t;

examples/share-locales-in-workspace/my-app1/Cargo.toml examples/share-in-workspace/crates/my-app1/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ name = "my-app1"
44
version = "0.1.0"
55

66
[dependencies]
7-
rust-i18n = { path = "../../.." }
8-
my-i18n.workspace = true
7+
rust-i18n.workspace = true
8+
i18n.workspace = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use i18n::t;
2+
3+
i18n::init!();
4+
5+
#[allow(dead_code)]
6+
fn assert_messages() {
7+
assert_eq!(
8+
t!("welcome", locale = "en"),
9+
"Rust I18n Example for share locales in entire workspace."
10+
);
11+
assert_eq!(
12+
t!("welcome", locale = "zh-CN"),
13+
"Rust I18n 示例,用于在整个工作区中共享本地化。"
14+
);
15+
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
#[test]
20+
fn test_load_str() {
21+
super::assert_messages();
22+
}
23+
}

examples/share-locales-in-workspace/my-app2/Cargo.toml examples/share-in-workspace/crates/my-app2/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ name = "my-app2"
44
version = "0.1.0"
55

66
[dependencies]
7-
rust-i18n = { path = "../../.." }
8-
my-i18n.workspace = true
7+
rust-i18n.workspace = true
8+
i18n.workspace = true

examples/share-locales-in-workspace/my-app2/src/lib.rs examples/share-in-workspace/crates/my-app2/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
rust_i18n::i18n!(backend = my_i18n::I18nBackend);
1+
i18n::init!();
22

33
#[cfg(test)]
44
mod tests {

examples/share-locales-in-workspace/Cargo.toml

-11
This file was deleted.

examples/share-locales-in-workspace/my-app1/src/lib.rs

-18
This file was deleted.

0 commit comments

Comments
 (0)