From fdd3efeb5780aa0bfef172e0621d89ffc1eb7655 Mon Sep 17 00:00:00 2001 From: Tom Lienard Date: Sun, 24 Dec 2023 08:15:04 +0100 Subject: [PATCH] fix: warn in changeLocale shows defined locales --- .../src/app/client/create-i18n-provider-client.tsx | 6 ++++-- .../src/app/client/create-use-change-locale.ts | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/next-international/src/app/client/create-i18n-provider-client.tsx b/packages/next-international/src/app/client/create-i18n-provider-client.tsx index 1a97543..e13e493 100644 --- a/packages/next-international/src/app/client/create-i18n-provider-client.tsx +++ b/packages/next-international/src/app/client/create-i18n-provider-client.tsx @@ -11,9 +11,8 @@ type I18nProviderProps = Omit; type I18nProviderWrapperProps = { locale: string; fallback?: ReactNode; - children: ReactNode; - importLocale: Promise>; + children: ReactNode; }; export const localesCache = new Map>(); @@ -25,9 +24,11 @@ export function createI18nProviderClient( ) { function I18nProvider({ locale, importLocale, children }: I18nProviderProps) { const clientLocale = (localesCache.get(locale) ?? use(importLocale).default) as Record; + if (!localesCache.has(locale)) { localesCache.set(locale, clientLocale); } + const value = useMemo( () => ({ localeContent: flattenLocale(clientLocale), @@ -42,6 +43,7 @@ export function createI18nProviderClient( return function I18nProviderWrapper({ locale, fallback, children }: I18nProviderWrapperProps) { const importFnLocale = locales[locale as keyof typeof locales]; + if (!importFnLocale) { error(`The locale '${locale}' is not supported. Defined locales are: [${Object.keys(locales).join(', ')}].`); notFound(); diff --git a/packages/next-international/src/app/client/create-use-change-locale.ts b/packages/next-international/src/app/client/create-use-change-locale.ts index 1ee0256..d866050 100644 --- a/packages/next-international/src/app/client/create-use-change-locale.ts +++ b/packages/next-international/src/app/client/create-use-change-locale.ts @@ -32,13 +32,15 @@ export function createUseChangeLocale( return function changeLocale(newLocale: LocalesKeys) { const importFnLocale = locales[newLocale as keyof typeof locales]; + if (!importFnLocale) { - warn(`The locale '${newLocale}' is not supported.`); + warn(`The locale '${newLocale}' is not supported. Defined locales are: [${Object.keys(locales).join(', ')}].`); return; } importFnLocale().then(module => { localesCache.set(newLocale as string, module.default); + push(`/${newLocale}${pathWithoutLocale}${finalSearchParams}`); refresh(); });