From afcd189a26e0a74da89c611ff4ec01e263e0c698 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 17 Apr 2024 15:27:49 +0200 Subject: [PATCH] fix: switchLocalePath when not in dev mode by handling trailing slash (close #13) --- .changeset/tall-lizards-punch.md | 5 +++++ package/src/stubs/virtual.mjs | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/tall-lizards-punch.md diff --git a/.changeset/tall-lizards-punch.md b/.changeset/tall-lizards-punch.md new file mode 100644 index 0000000..c0ee458 --- /dev/null +++ b/.changeset/tall-lizards-punch.md @@ -0,0 +1,5 @@ +--- +"@astrolicious/i18n": patch +--- + +Fixes `switchLocalePath` (and anything that depends on it like `getSwitcherData`) when not in dev mode diff --git a/package/src/stubs/virtual.mjs b/package/src/stubs/virtual.mjs index 6df90c9..193f53c 100644 --- a/package/src/stubs/virtual.mjs +++ b/package/src/stubs/virtual.mjs @@ -107,6 +107,12 @@ const _dir = (locale) => { return rtlLocales.includes(locale) ? "rtl" : "ltr"; }; +/** + * @param {string} path + */ +const _withoutTrailingSlash = (path) => + path.endsWith("/") ? path.slice(0, -1) : path; + export const t = (...args) => { _envCheck("t", { clientFeatures: ["data", "translations"] }); const config = _getConfig(); @@ -221,7 +227,11 @@ export const switchLocalePath = (locale) => { // Static let currentLocaleRoute = currentLocaleRoutes .filter((route) => route.params.length === 0) - .find((route) => route.injectedRoute.pattern === config.paths.pathname); + .find( + (route) => + route.injectedRoute.pattern === + _withoutTrailingSlash(config.paths.pathname), + ); // Dynamic if (!currentLocaleRoute) {