Skip to content

Commit

Permalink
Merge pull request #9 from wendajiang/main
Browse files Browse the repository at this point in the history
feat: use once_cell instead of lazy_static
  • Loading branch information
wendajiang authored May 6, 2022
2 parents 0ce5b7a + 1a4ea66 commit c36a845
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ version = "0.5.3-alpha.0"
anyhow = {version = "1", optional = true}
clap = {version = "2.32", optional = true}
itertools = {version = "0.10.3", optional = true}
lazy_static = "1.4.0"
once_cell = "1.10.0"
quote = {version = "1", optional = true}
rust-i18n-extract = {path = "./crates/extract", version = "0.3", optional = true}
rust-i18n-macro = {path = "./crates/macro", version = "0.3"}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Add crate dependencies in your Cargo.toml and setup I18n config:

```toml
[dependencies]
lazy_static = "1.4.0"
once_cell = "1.10.0"
rust-i18n = "0"

[package.metadata.i18n]
Expand Down
2 changes: 1 addition & 1 deletion crates/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version = "0.3.0"

[dependencies]
glob = "0.3"
lazy_static = "1.4.0"
once_cell = "1.10.0"
proc-macro2 = "1.0"
quote = "1.0.2"
rust-i18n-support = {path = "../support", version = "0.3"}
Expand Down
10 changes: 4 additions & 6 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ fn generate_code(translations: HashMap<String, String>) -> proc_macro2::TokenStr

// result
quote! {
lazy_static::lazy_static! {
static ref LOCALES: std::collections::HashMap<&'static str, &'static str> = rust_i18n::map! [
#(#locales)*
"" => ""
];
}
static LOCALES: once_cell::sync::Lazy<std::collections::HashMap<&'static str, &'static str>> = once_cell::sync::Lazy::new(|| rust_i18n::map! [
#(#locales)*
"" => ""
]);


pub fn translate(locale: &str, key: &str) -> String {
Expand Down
2 changes: 1 addition & 1 deletion crates/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.3.0"

[dependencies]
glob = "0.3"
lazy_static = "1.4.0"
once_cell = "1.10.0"
proc-macro2 = "1.0"
serde = "1"
serde_json = "1"
Expand Down
2 changes: 1 addition & 1 deletion examples/foo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lazy_static = "1.4.0"
once_cell = "1.10.0"
rust-i18n = {path = "../../../rust-i18n"}

[package.metadata.i18n]
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Add crate dependencies in your Cargo.toml:
```toml
[dependencies]
lazy_static = "1.4.0"
once_cell = "1.10.0"
rust-i18n = "0"
```
Expand Down Expand Up @@ -67,13 +67,12 @@ rust_i18n::locale();
```
*/
// include!(concat!(env!("OUT_DIR"), "/i18n.rs"));
use once_cell::sync::Lazy;
use std::sync::Mutex;

pub use rust_i18n_macro::i18n;

lazy_static::lazy_static! {
static ref CURRENT_LOCALE: Mutex<String> = Mutex::new(String::from("en"));
}
static CURRENT_LOCALE: Lazy<Mutex<String>> = Lazy::new(|| Mutex::new(String::from("en")));

pub fn set_locale(locale: &str) {
let mut current_locale = CURRENT_LOCALE.lock().unwrap();
Expand Down

0 comments on commit c36a845

Please sign in to comment.