Skip to content

Commit

Permalink
fix: switchLocalePath when not in dev mode by handling trailing slash (
Browse files Browse the repository at this point in the history
…close #13)
  • Loading branch information
florian-lefebvre committed Apr 17, 2024
1 parent dead654 commit afcd189
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tall-lizards-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrolicious/i18n": patch
---

Fixes `switchLocalePath` (and anything that depends on it like `getSwitcherData`) when not in dev mode
12 changes: 11 additions & 1 deletion package/src/stubs/virtual.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit afcd189

Please sign in to comment.