From f2aa96e7b7282a01e7cd35a585068199f9181e82 Mon Sep 17 00:00:00 2001 From: arctic-hen7 Date: Sat, 7 Jan 2023 17:22:09 +1100 Subject: [PATCH] fix(i18n): fixed `t!` macro with multiple interpolations BREAKING CHANGE: `cx` now comes first in `t!` and `link!` invocations (following Sycamore convention) --- examples/core/i18n/src/templates/about.rs | 2 +- examples/core/i18n/src/templates/index.rs | 6 +++--- examples/core/i18n/src/templates/post.rs | 4 ++-- examples/core/preload/src/templates/about.rs | 4 ++-- examples/core/preload/src/templates/index.rs | 4 ++-- examples/website/i18n/src/main.rs | 2 +- packages/perseus/src/translator/mod.rs | 8 ++++---- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/core/i18n/src/templates/about.rs b/examples/core/i18n/src/templates/about.rs index 65d151c369..5518341559 100644 --- a/examples/core/i18n/src/templates/about.rs +++ b/examples/core/i18n/src/templates/about.rs @@ -3,7 +3,7 @@ use sycamore::prelude::*; fn about_page(cx: Scope) -> View { view! { cx, - p { (t!("about", cx)) } + p { (t!(cx, "about")) } p { ( if !G::IS_BROWSER { diff --git a/examples/core/i18n/src/templates/index.rs b/examples/core/i18n/src/templates/index.rs index e432a75e0c..bd66a521f6 100644 --- a/examples/core/i18n/src/templates/index.rs +++ b/examples/core/i18n/src/templates/index.rs @@ -5,10 +5,10 @@ fn index_page(cx: Scope) -> View { let username = "User"; view! { cx, - p { (t!("hello", { + p { (t!(cx, "hello", { "user" = username - }, cx)) } - a(href = link!("/about", cx)) { "About" } + })) } + a(href = link!(cx, "/about")) { "About" } } } diff --git a/examples/core/i18n/src/templates/post.rs b/examples/core/i18n/src/templates/post.rs index 1029ab85c5..dcc93456f8 100644 --- a/examples/core/i18n/src/templates/post.rs +++ b/examples/core/i18n/src/templates/post.rs @@ -17,9 +17,9 @@ fn post_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, props: &'a PostPageStateRx) p { (props.content.get()) } - a(href = link!("/post", cx)) { "Root post page" } + a(href = link!(cx, "/post")) { "Root post page" } br() - a(href = link!("/post/blah/test/blah", cx)) { "Complex post page" } + a(href = link!(cx, "/post/blah/test/blah")) { "Complex post page" } } } diff --git a/examples/core/preload/src/templates/about.rs b/examples/core/preload/src/templates/about.rs index 24a1e13aec..e917d54409 100644 --- a/examples/core/preload/src/templates/about.rs +++ b/examples/core/preload/src/templates/about.rs @@ -4,9 +4,9 @@ use sycamore::view::View; fn about_page(cx: Scope) -> View { view! { cx, - p { (t!("about-msg", cx)) } + p { (t!(cx, "about-msg")) } - a(id = "index", href = link!("", cx)) { (t!("about-index-link", cx)) } + a(id = "index", href = link!(cx, "")) { (t!(cx, "about-index-link")) } } } diff --git a/examples/core/preload/src/templates/index.rs b/examples/core/preload/src/templates/index.rs index f50ae1e617..b291bd31bd 100644 --- a/examples/core/preload/src/templates/index.rs +++ b/examples/core/preload/src/templates/index.rs @@ -20,9 +20,9 @@ fn index_page(cx: Scope) -> View { } view! { cx, - p { (t!("index-msg", cx)) } + p { (t!(cx, "index-msg")) } - a(id = "about", href = link!("about", cx)) { (t!("index-about-link", cx)) } + a(id = "about", href = link!(cx, "about")) { (t!(cx, "index-about-link")) } a(id = "fr-about", href = "fr-FR/about") { "About (French)" } a(id = "en-about", href = "en-US/about") { "About (English)" } } diff --git a/examples/website/i18n/src/main.rs b/examples/website/i18n/src/main.rs index 02c8e8232f..f74eae3825 100644 --- a/examples/website/i18n/src/main.rs +++ b/examples/website/i18n/src/main.rs @@ -20,7 +20,7 @@ pub fn main() -> PerseusApp { // used. fn index_page(cx: Scope) -> View { view! { cx, - h1 { (t!("greeting", cx)) } + h1 { (t!(cx, "greeting")) } } } diff --git a/packages/perseus/src/translator/mod.rs b/packages/perseus/src/translator/mod.rs index 2a3dec8e25..4ffb0160ab 100644 --- a/packages/perseus/src/translator/mod.rs +++ b/packages/perseus/src/translator/mod.rs @@ -102,14 +102,14 @@ pub use DUMMY_TRANSLATOR_FILE_EXT as TRANSLATOR_FILE_EXT; #[macro_export] macro_rules! t { // When there are no arguments to interpolate - ($id:expr, $cx:expr) => { + ($cx:expr, $id:expr) => { $crate::i18n::t_macro_backend($id, $cx) }; // When there are arguments to interpolate - ($id:expr, { + ($cx:expr, $id:expr, { // NOTE Using a colon here leads to literally impossible to solve cast errors based on compiler misinterpretations $($key:literal = $value:expr),+ - }, $cx:expr) => {{ + }) => {{ let mut args = $crate::i18n::TranslationArgs::new(); $( args.set($key, $value); @@ -122,7 +122,7 @@ macro_rules! t { /// scope provided to the relevant Perseus template. #[macro_export] macro_rules! link { - ($url:expr, $cx:expr) => { + ($cx:expr, $url:expr) => { $crate::i18n::link_macro_backend($url, $cx) }; }