-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
13 changed files
with
68 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
4 changes: 2 additions & 2 deletions
4
...e-locales-in-workspace/my-i18n/Cargo.toml → ...share-in-workspace/crates/i18n/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e-locales-in-workspace/my-app2/src/lib.rs → ...re-in-workspace/crates/my-app2/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.