Skip to content

Commit

Permalink
fix: warn in changeLocale shows defined locales
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Dec 24, 2023
1 parent f3c9ca0 commit fdd3efe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ type I18nProviderProps = Omit<I18nProviderWrapperProps, 'fallback'>;
type I18nProviderWrapperProps = {
locale: string;
fallback?: ReactNode;
children: ReactNode;

importLocale: Promise<Record<string, unknown>>;
children: ReactNode;
};

export const localesCache = new Map<string, Record<string, unknown>>();
Expand All @@ -25,9 +24,11 @@ export function createI18nProviderClient<Locale extends BaseLocale>(
) {
function I18nProvider({ locale, importLocale, children }: I18nProviderProps) {
const clientLocale = (localesCache.get(locale) ?? use(importLocale).default) as Record<string, unknown>;

if (!localesCache.has(locale)) {
localesCache.set(locale, clientLocale);
}

const value = useMemo(
() => ({
localeContent: flattenLocale<Locale>(clientLocale),
Expand All @@ -42,6 +43,7 @@ export function createI18nProviderClient<Locale extends BaseLocale>(

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ export function createUseChangeLocale<LocalesKeys>(

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();
});
Expand Down

0 comments on commit fdd3efe

Please sign in to comment.