Skip to content

Commit

Permalink
Refactor Tr
Browse files Browse the repository at this point in the history
  • Loading branch information
varphone committed Jan 21, 2024
1 parent 30c14fd commit f43d0a9
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions crates/macro/src/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ impl Tr {
.iter()
.map(|v| quote! { format!("{}", #v) })
.collect();
let logging = if cfg!(feature = "log-tr-dyn") {
quote! {
log::warn!("tr: missing: {} => {:?} @ {}:{}", msg_key, msg_val, file!(), line!());
}
} else {
quote! {}
};
match msg_kind {
Messagekind::Literal => {
if self.args.is_empty() {
Expand All @@ -221,13 +228,6 @@ impl Tr {
}
}
Messagekind::ExprCall | Messagekind::ExprClosure | Messagekind::ExprMacro => {
let logging = if cfg!(feature = "log-tr-dyn") {
quote! {
log::warn!("tr: missing: {} => {:?} @ {}:{}", msg_key, msg_val, file!(), line!());
}
} else {
quote! {}
};
if self.args.is_empty() {
quote! {
{
Expand Down Expand Up @@ -264,36 +264,32 @@ impl Tr {
| Messagekind::ExprReference
| Messagekind::ExprUnary
| Messagekind::Ident => {
let logging = if cfg!(feature = "log-tr-dyn") {
quote! {
log::warn!("tr: missing: {} => {:?} @ {}:{}", msg_key, #msg_val, file!(), line!());
}
} else {
quote! {}
};
if self.args.is_empty() {
quote! {
{
let msg_key = rust_i18n::TrKey::tr_key(&#msg_key);
let msg_val = #msg_val;
if let Some(translated) = crate::_rust_i18n_try_translate(#locale, &msg_key) {
translated
} else {
rust_i18n::CowStr::from(#msg_val).into_inner()
#logging
rust_i18n::CowStr::from(msg_val).into_inner()
}
}
}
} else {
quote! {
{
let msg_key = rust_i18n::TrKey::tr_key(&#msg_key);
let msg_val = #msg_val;
let keys = &[#(#keys),*];
let values = &[#(#values),*];
if let Some(translated) = crate::_rust_i18n_try_translate(#locale, &msg_key) {
let replaced = rust_i18n::replace_patterns(&translated, keys, values);
std::borrow::Cow::from(replaced)
} else {
#logging
let replaced = rust_i18n::replace_patterns(&rust_i18n::CowStr::from(#msg_val).into_inner(), keys, values);
let replaced = rust_i18n::replace_patterns(&rust_i18n::CowStr::from(msg_val).into_inner(), keys, values);
std::borrow::Cow::from(replaced)
}
}
Expand Down

0 comments on commit f43d0a9

Please sign in to comment.