From 448a6096cf39c8f22655021df5646fc198e42148 Mon Sep 17 00:00:00 2001 From: Lucas Yang Date: Thu, 19 Dec 2024 16:46:51 +0800 Subject: [PATCH 1/2] Fix incorrect matching second dynamic route --- .changeset/red-lies-play.md | 5 ++ package/assets/stubs/virtual.mjs | 23 ++++++--- playground/src/layouts/Layout.astro | 1 + playground/src/locales/en/common.json | 3 +- playground/src/locales/fr/common.json | 3 +- playground/src/routes/user.astro | 32 +++++++++++++ playground/src/routes/user/[slug].astro | 63 +++++++++++++++++++++++++ 7 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 .changeset/red-lies-play.md create mode 100644 playground/src/routes/user.astro create mode 100644 playground/src/routes/user/[slug].astro diff --git a/.changeset/red-lies-play.md b/.changeset/red-lies-play.md new file mode 100644 index 0000000..b1b9f3f --- /dev/null +++ b/.changeset/red-lies-play.md @@ -0,0 +1,5 @@ +--- +"@astrolicious/i18n": patch +--- + +Fix incorrect matching second dynamic route diff --git a/package/assets/stubs/virtual.mjs b/package/assets/stubs/virtual.mjs index f5cc894..1bd89ba 100644 --- a/package/assets/stubs/virtual.mjs +++ b/package/assets/stubs/virtual.mjs @@ -237,13 +237,22 @@ export const switchLocalePath = (locale) => { if (!currentLocaleRoute) { currentLocaleRoute = currentLocaleRoutes .filter((route) => route.params.length > 0) - .find( - (route) => - JSON.stringify(route.params.sort()) === - JSON.stringify( - Object.keys(config.paths.dynamicParams?.[locale] ?? {}).sort(), - ), - ); + .find((route) => { + // Convert the route pattern to a regex pattern + + // Replace all dynamic params with the ".*" regex pattern + let pattern = route.injectedRoute.pattern.replace(/[*.]/g, "\\$&"); + pattern = Object.keys( + config.paths.dynamicParams?.[locale] ?? {}, + ).reduce((acc, key) => acc.replace(`[${key}]`, ".*"), pattern); + + // Escape all special characters + pattern = pattern.replace(/[-[\]{}()+?,\\^$|#\s]/g, "\\$&"); + + return new RegExp(`^${pattern}$`).test( + _withoutTrailingSlash(config.paths.pathname), + ); + }); } // Fallback diff --git a/playground/src/layouts/Layout.astro b/playground/src/layouts/Layout.astro index d045cf2..a725dce 100644 --- a/playground/src/layouts/Layout.astro +++ b/playground/src/layouts/Layout.astro @@ -30,6 +30,7 @@ const { title } = Astro.props; {t("home:title")} {t("about")} {t("blog")} + {t("user")} diff --git a/playground/src/locales/en/common.json b/playground/src/locales/en/common.json index da32227..32a320e 100644 --- a/playground/src/locales/en/common.json +++ b/playground/src/locales/en/common.json @@ -1,5 +1,6 @@ { "home": "Home", "about": "About", - "blog": "The blog" + "blog": "The blog", + "user": "The user" } diff --git a/playground/src/locales/fr/common.json b/playground/src/locales/fr/common.json index f2e6e59..689cc26 100644 --- a/playground/src/locales/fr/common.json +++ b/playground/src/locales/fr/common.json @@ -1,5 +1,6 @@ { "home": "Accueil", "about": "A propos", - "blog": "Le blog" + "blog": "Le blog", + "user": "Utilisateur" } diff --git a/playground/src/routes/user.astro b/playground/src/routes/user.astro new file mode 100644 index 0000000..6b4dc38 --- /dev/null +++ b/playground/src/routes/user.astro @@ -0,0 +1,32 @@ +--- +import { getLocale, getLocalePath, t } from "i18n:astro"; +import Layout from "~/layouts/Layout.astro"; + +const title = t("user"); + +const slugs = [ + { + en: "a", + fr: "d", + }, + { + en: "b", + fr: "e", + }, + { + en: "c", + fr: "f", + }, +]; + +const currentLocaleSlugs = slugs.map((e) => e[getLocale()]); +--- + + +

{title}

+ { + currentLocaleSlugs.map((slug) => ( + {slug} + )) + } +
diff --git a/playground/src/routes/user/[slug].astro b/playground/src/routes/user/[slug].astro new file mode 100644 index 0000000..34b7996 --- /dev/null +++ b/playground/src/routes/user/[slug].astro @@ -0,0 +1,63 @@ +--- +import { + getDefaultLocalePlaceholder, + getLocalePlaceholder, + getLocalesPlaceholder, + setDynamicParams, + t, +} from "i18n:astro"; +import sitemap from "i18n:astro/sitemap"; +import type { GetStaticPaths } from "astro"; +import Layout from "~/layouts/Layout.astro"; + +export const getStaticPaths = (() => { + const locale = getLocalePlaceholder(); + const locales = getLocalesPlaceholder(); + const defaultLocale = getDefaultLocalePlaceholder(); + console.log({ locale, locales, defaultLocale }); + + const slugs = [ + { + en: "a", + fr: "d", + }, + { + en: "b", + fr: "e", + }, + { + en: "c", + fr: "f", + }, + ]; + + return slugs.map((slug) => { + const dynamicParams = Object.entries(slug).map(([locale, slug]) => ({ + locale, + params: { slug }, + })); + sitemap({ + dynamicParams, + }); + return { + params: { + slug: slug[locale], + }, + props: { + dynamicParams, + }, + }; + }); +}) satisfies GetStaticPaths; + +const { slug } = Astro.params; +const { dynamicParams } = Astro.props; + +setDynamicParams(dynamicParams); + +const title = t("user"); +--- + + +

{title} {slug}

+
From aba84156944ff3819878b8e0e40474b2da53e82b Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Tue, 24 Dec 2024 10:32:20 +0100 Subject: [PATCH 2/2] chore: changeset --- .changeset/red-lies-play.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/red-lies-play.md b/.changeset/red-lies-play.md index b1b9f3f..badeebd 100644 --- a/.changeset/red-lies-play.md +++ b/.changeset/red-lies-play.md @@ -2,4 +2,4 @@ "@astrolicious/i18n": patch --- -Fix incorrect matching second dynamic route +Fixes matching of similar dynamic parameters