Skip to content

Commit

Permalink
fix(i18n): fixed t! macro with multiple interpolations
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `cx` now comes first in `t!` and `link!`
invocations (following Sycamore convention)
  • Loading branch information
arctic-hen7 committed Jan 7, 2023
1 parent 7c63355 commit f2aa96e
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/core/i18n/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sycamore::prelude::*;

fn about_page<G: Html>(cx: Scope) -> View<G> {
view! { cx,
p { (t!("about", cx)) }
p { (t!(cx, "about")) }
p {
(
if !G::IS_BROWSER {
Expand Down
6 changes: 3 additions & 3 deletions examples/core/i18n/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ fn index_page<G: Html>(cx: Scope) -> View<G> {
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" }
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/core/i18n/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/core/preload/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use sycamore::view::View;

fn about_page<G: Html>(cx: Scope) -> View<G> {
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")) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/core/preload/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ fn index_page<G: Html>(cx: Scope) -> View<G> {
}

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)" }
}
Expand Down
2 changes: 1 addition & 1 deletion examples/website/i18n/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn main<G: Html>() -> PerseusApp<G> {
// used.
fn index_page<G: Html>(cx: Scope) -> View<G> {
view! { cx,
h1 { (t!("greeting", cx)) }
h1 { (t!(cx, "greeting")) }
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/perseus/src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
};
}

0 comments on commit f2aa96e

Please sign in to comment.