From fd7174179881a19e3573fceb9c6e903923644761 Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Tue, 26 Nov 2024 10:59:58 +0100 Subject: [PATCH] fix: Follow-up for #1573 to also handle the case when a non-default locale is in use (#1578) Fixes #1568 --- .../navigation/react-client/createNavigation.test.tsx | 10 +++++++++- .../src/navigation/react-client/useBasePathname.tsx | 3 +-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/next-intl/src/navigation/react-client/createNavigation.test.tsx b/packages/next-intl/src/navigation/react-client/createNavigation.test.tsx index a3c1d805e..e8aa6a9a3 100644 --- a/packages/next-intl/src/navigation/react-client/createNavigation.test.tsx +++ b/packages/next-intl/src/navigation/react-client/createNavigation.test.tsx @@ -550,7 +550,8 @@ describe("localePrefix: 'as-needed', custom `prefixes`", () => { localePrefix: { mode: 'as-needed', prefixes: { - en: '/uk' + en: '/english', + de: '/deutsch' } } }); @@ -563,6 +564,13 @@ describe("localePrefix: 'as-needed', custom `prefixes`", () => { renderPathname(); screen.getByText('/about'); }); + + it('is tolerant when a locale is used in the pathname for a non-default locale', () => { + mockCurrentLocale('de'); + mockLocation({pathname: '/de/about'}); + renderPathname(); + screen.getByText('/about'); + }); }); describe("localePrefix: 'as-needed', with `basePath` and `domains`", () => { diff --git a/packages/next-intl/src/navigation/react-client/useBasePathname.tsx b/packages/next-intl/src/navigation/react-client/useBasePathname.tsx index 5368a648f..bd52dffc8 100644 --- a/packages/next-intl/src/navigation/react-client/useBasePathname.tsx +++ b/packages/next-intl/src/navigation/react-client/useBasePathname.tsx @@ -47,7 +47,6 @@ export default function useBasePathname< unlocalizedPathname = unprefixPathname(pathname, prefix); } else if ( config.localePrefix.mode === 'as-needed' && - config.defaultLocale === locale && config.localePrefix.prefixes ) { // Workaround for https://github.com/vercel/next.js/issues/73085 @@ -58,5 +57,5 @@ export default function useBasePathname< } return unlocalizedPathname; - }, [config.defaultLocale, config.localePrefix, locale, pathname]); + }, [config.localePrefix, locale, pathname]); }