Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes matching of similar dynamic parameters #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-lies-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrolicious/i18n": patch
---

Fixes matching of similar dynamic parameters
23 changes: 16 additions & 7 deletions package/assets/stubs/virtual.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions playground/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const { title } = Astro.props;
<a href={getLocalePath("/")}>{t("home:title")}</a>
<a href={getLocalePath("/about")}>{t("about")}</a>
<a href={getLocalePath("/blog")}>{t("blog")}</a>
<a href={getLocalePath("/user")}>{t("user")}</a>
</div>
<LocaleSwitcher />
</header>
Expand Down
3 changes: 2 additions & 1 deletion playground/src/locales/en/common.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"home": "Home",
"about": "About",
"blog": "The blog"
"blog": "The blog",
"user": "The user"
}
3 changes: 2 additions & 1 deletion playground/src/locales/fr/common.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"home": "Accueil",
"about": "A propos",
"blog": "Le blog"
"blog": "Le blog",
"user": "Utilisateur"
}
32 changes: 32 additions & 0 deletions playground/src/routes/user.astro
Original file line number Diff line number Diff line change
@@ -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()]);
---

<Layout {title}>
<h1>{title}</h1>
{
currentLocaleSlugs.map((slug) => (
<a href={getLocalePath("/user/[slug]", { slug })} class="block p-1 underline">{slug}</a>
))
}
</Layout>
63 changes: 63 additions & 0 deletions playground/src/routes/user/[slug].astro
Original file line number Diff line number Diff line change
@@ -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");
---

<Layout {title}>
<h1>{title} {slug}</h1>
</Layout>
Loading