diff --git a/Cargo.toml b/Cargo.toml index 35c4691..d375b9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ toml = "0.5.8" [dev-dependencies] foo = {path = "examples/foo"} -criterion = "0.3" +criterion = "0.5" [features] default = ["rust-i18n-extract", "clap", "anyhow", "quote", "itertools"] diff --git a/README.md b/README.md index fdb06c1..94c52e5 100644 --- a/README.md +++ b/README.md @@ -317,8 +317,8 @@ You can add [i18n-ally-custom-framework.yml](https://github.com/longbridgeapp/ru Benchmark `t!` method, result on Apple M1: -``` -t time: [110.94 ns 113.66 ns 117.06 ns] +```bash +t time: [100.91 ns 101.06 ns 101.24 ns] t_with_args time: [495.56 ns 497.88 ns 500.64 ns] ``` diff --git a/benches/bench.rs b/benches/bench.rs index 0ea5305..f2312d7 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -6,13 +6,15 @@ use criterion::{criterion_group, criterion_main, Criterion}; fn bench_t(c: &mut Criterion) { c.bench_function("t", |b| b.iter(|| t!("hello"))); -} -fn bench_t_with_args(c: &mut Criterion) { c.bench_function("t_with_args", |b| { b.iter(|| t!("a.very.nested.message", name = "Jason", msg = "Bla bla")) }); + + c.bench_function("t_with_args (str)", |b| { + b.iter(|| t!("a.very.nested.message", "name" = "Jason", "msg" = "Bla bla")) + }); } -criterion_group!(benches, bench_t, bench_t_with_args); +criterion_group!(benches, bench_t); criterion_main!(benches); diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index a565398..89ade47 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -166,8 +166,6 @@ fn generate_code( /// Get I18n text by locale and key pub fn _rust_i18n_translate(locale: &str, key: &str) -> String { - let target_key = format!("{}.{}", locale, key); - if let Some(value) = _RUST_I18N_BACKEND.translate(locale, key) { return value.to_string(); } @@ -179,7 +177,7 @@ fn generate_code( } } - return target_key + return format!("{}.{}", locale, key); } pub fn _rust_i18n_available_locales() -> Vec<&'static str> { diff --git a/src/lib.rs b/src/lib.rs index 521ce37..ab1cd1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,20 +45,21 @@ macro_rules! t { crate::_rust_i18n_translate(rust_i18n::locale().as_str(), $key) }; - // t!("foo", locale="en") - ($key:expr, locale=$locale:expr) => { + // t!("foo", locale = "en") + ($key:expr, locale = $locale:expr) => { crate::_rust_i18n_translate($locale, $key) }; - // t!("foo", locale="en", a=1, b="Foo") - ($key:expr, locale=$locale:expr, $($var_name:tt = $var_val:expr),+ $(,)?) => { + // t!("foo", locale = "en", a = 1, b = "Foo") + ($key:expr, locale = $locale:expr, $($var_name:tt = $var_val:expr),+ $(,)?) => { { let mut message = crate::_rust_i18n_translate($locale, $key); + $( - let var = stringify!($var_name).trim_matches('"'); - let mut holder = std::string::String::from("%{"); - holder.push_str(var); - holder.push('}'); + // Get the variable name as a string, and remove quotes surrounding the variable name + let var_name = stringify!($var_name).trim_matches('"'); + // Make a holder string to replace the variable name with: %{var_name} + let holder = format!("%{{{var_name}}}"); message = message.replace(&holder, &format!("{}", $var_val)); )+ @@ -66,7 +67,7 @@ macro_rules! t { } }; - // t!("foo %{a} %{b}", a="bar", b="baz") + // t!("foo %{a} %{b}", a = "bar", b = "baz") ($key:expr, $($var_name:tt = $var_val:expr),+ $(,)?) => { { t!($key, locale = &rust_i18n::locale(), $($var_name = $var_val),*) @@ -74,14 +75,14 @@ macro_rules! t { }; // t!("foo %{a} %{b}", locale = "en", "a" => "bar", "b" => "baz") - ($key:expr, locale = $locale:expr, $($var_name:expr => $var_val:expr),+ $(,)?) => { + ($key:expr, locale = $locale:expr, $($var_name:tt => $var_val:expr),+ $(,)?) => { { t!($key, locale = $locale, $($var_name = $var_val),*) } }; // t!("foo %{a} %{b}", "a" => "bar", "b" => "baz") - ($key:expr, $($var_name:expr => $var_val:expr),+ $(,)?) => { + ($key:expr, $($var_name:tt => $var_val:expr),+ $(,)?) => { { t!($key, locale = &rust_i18n::locale(), $($var_name = $var_val),*) }