Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: execute function in I18nProviderWrapper instead of I18nProvider #316

Merged
merged 7 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { notFound } from 'next/navigation';
import type { BaseLocale, ImportedLocales } from 'international-types';
import { notFound } from 'next/navigation';
import type { Context, ReactNode } from 'react';
import React, { Suspense, use, useMemo } from 'react';
import { flattenLocale } from '../../common/flatten-locale';
Expand All @@ -11,6 +11,7 @@ type I18nProviderProps = Omit<I18nProviderWrapperProps, 'fallback'>;
type I18nProviderWrapperProps = {
locale: string;
fallback?: ReactNode;
importLocale: Promise<Record<string, unknown>>;
children: ReactNode;
};

Expand All @@ -21,18 +22,10 @@ export function createI18nProviderClient<Locale extends BaseLocale>(
locales: ImportedLocales,
fallbackLocale?: Record<string, unknown>,
) {
function I18nProvider({ locale, children }: I18nProviderProps) {
let clientLocale: any = localesCache.get(locale);

if (!clientLocale) {
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();
}
function I18nProvider({ locale, importLocale, children }: I18nProviderProps) {
const clientLocale = (localesCache.get(locale) ?? use(importLocale).default) as Record<string, unknown>;

clientLocale = use(newLocale()).default;
if (!localesCache.has(locale)) {
localesCache.set(locale, clientLocale);
}

Expand All @@ -49,9 +42,18 @@ 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();
}

return (
<Suspense fallback={fallback}>
<I18nProvider locale={locale}>{children}</I18nProvider>
<I18nProvider locale={locale} importLocale={importFnLocale()}>
{children}
</I18nProvider>
</Suspense>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useRouter, usePathname, useSearchParams } from 'next/navigation';
import type { I18nChangeLocaleConfig, I18nClientConfig } from '../../types';
import { warn } from '../../helpers/log';
import type { ImportedLocales } from 'international-types';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import type { I18nChangeLocaleConfig, I18nClientConfig } from '../../types';
import { localesCache } from './create-i18n-provider-client';

export function createUseChangeLocale<LocalesKeys>(
Expand Down Expand Up @@ -30,7 +31,14 @@ export function createUseChangeLocale<LocalesKeys>(
}

return function changeLocale(newLocale: LocalesKeys) {
locales[newLocale as keyof typeof locales]().then(module => {
const importFnLocale = locales[newLocale as keyof typeof locales];

if (!importFnLocale) {
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}`);
Expand Down