Skip to content

Commit

Permalink
Add examples for explain use Backend for share locales dict in entire…
Browse files Browse the repository at this point in the history
… workspace.
  • Loading branch information
huacnlee committed Sep 4, 2023
1 parent 737370d commit ba2c7cb
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
Cargo.lock
debug.txt
.idea/
.DS_Store
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,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
2 changes: 2 additions & 0 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ fn generate_code(
use rust_i18n::BackendExt;

/// I18n backend instance
///
/// [PUBLIC] This is a public API, and as an example in examples/
static _RUST_I18N_BACKEND: rust_i18n::once_cell::sync::Lazy<Box<dyn rust_i18n::Backend>> = rust_i18n::once_cell::sync::Lazy::new(|| {
let mut backend = rust_i18n::SimpleBackend::new();
#(#all_translations)*
Expand Down
11 changes: 11 additions & 0 deletions examples/share-locales-in-workspace/Cargo.toml
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" }
5 changes: 5 additions & 0 deletions examples/share-locales-in-workspace/locales/app.yml
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 示例,用於在整個工作區中共享本地化。
8 changes: 8 additions & 0 deletions examples/share-locales-in-workspace/my-app1/Cargo.toml
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
18 changes: 18 additions & 0 deletions examples/share-locales-in-workspace/my-app1/src/lib.rs
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 示例,用于在整个工作区中共享本地化。"
);
}
}
8 changes: 8 additions & 0 deletions examples/share-locales-in-workspace/my-app2/Cargo.toml
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
18 changes: 18 additions & 0 deletions examples/share-locales-in-workspace/my-app2/src/lib.rs
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 示例,用于在整个工作区中共享本地化。"
);
}
}
7 changes: 7 additions & 0 deletions examples/share-locales-in-workspace/my-i18n/Cargo.toml
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" }
20 changes: 20 additions & 0 deletions examples/share-locales-in-workspace/my-i18n/src/lib.rs
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
}
}
}

0 comments on commit ba2c7cb

Please sign in to comment.