-
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.
Add examples for explain use Backend for share locales dict in entire…
… workspace.
- Loading branch information
Showing
11 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ target | |
Cargo.lock | ||
debug.txt | ||
.idea/ | ||
.DS_Store |
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,11 @@ | ||
name = "sare-locales-in-workspace" | ||
|
||
[workspace] | ||
members = [ | ||
"my-app1", | ||
"my-app2", | ||
"my-i18n" | ||
] | ||
|
||
[workspace.dependencies] | ||
my-i18n = { path = "./my-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,5 @@ | ||
_version: 2 | ||
welcome: | ||
en: Rust I18n Example for share locales in entire workspace. | ||
zh-CN: Rust I18n 示例,用于在整个工作区中共享本地化。 | ||
zh-HK: Rust 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,8 @@ | ||
[package] | ||
edition = "2021" | ||
name = "my-app1" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
rust-i18n = { path = "../../../../rust-i18n" } | ||
my-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 示例,用于在整个工作区中共享本地化。" | ||
); | ||
} | ||
} |
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,8 @@ | ||
[package] | ||
edition = "2021" | ||
name = "my-app2" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
rust-i18n = { path = "../../../../rust-i18n" } | ||
my-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 示例,用于在整个工作区中共享本地化。" | ||
); | ||
} | ||
} |
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,7 @@ | ||
[package] | ||
edition = "2021" | ||
name = "my-i18n" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
rust-i18n = { path = "../../../../rust-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,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 | ||
} | ||
} | ||
} |