From fc4e60f7ba7a72e36a54ef4c92956ded958c5e28 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 24 Oct 2023 00:04:30 +0900 Subject: [PATCH] refactor: Update error message for clarity on defined locales (#252) Co-authored-by: QuiiBz --- .../src/app/client/create-i18n-provider-client.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 cd3fffc..462fbed 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 @@ -1,7 +1,9 @@ +import { notFound } from 'next/navigation'; import type { BaseLocale, ImportedLocales } from 'international-types'; import type { Context, ReactNode } from 'react'; import React, { Suspense, use, useMemo } from 'react'; import { flattenLocale } from '../../common/flatten-locale'; +import { error } from '../../helpers/log'; import type { LocaleContext } from '../../types'; type I18nProviderProps = Omit; @@ -23,7 +25,14 @@ export function createI18nProviderClient( let clientLocale: any = localesCache.get(locale); if (!clientLocale) { - clientLocale = use(locales[locale as keyof typeof locales]()).default; + const newLocale = locales[locale as keyof typeof locales]; + + if (!newLocale) { + error(`The locale '${locale}' is not supported. Defined locales are: [${Object.keys(locales).join(', ')}].`); + notFound(); + } + + clientLocale = use(newLocale()).default; localesCache.set(locale, clientLocale); }