Files
myeasycms-v2/packages/i18n/src/i18n-provider.tsx
giancarlo 8281de12a0 Update i18n settings and upgrade dependencies in lockfile
This commit refactors the i18n settings in `i18n.settings.ts` to improve language handling. In addition, it updates the `pnpm-lock.yaml` file to upgrade the lockfile version and several package dependencies. Most notably, it integrates `tailwind-merge` and `@tanstack/react-table` into various packages.
2024-04-20 13:17:38 +08:00

40 lines
941 B
TypeScript

'use client';
import type { InitOptions, i18n } from 'i18next';
let client: i18n;
type Resolver = (
lang: string,
namespace: string,
) => Promise<Record<string, string>>;
export function I18nProvider({
settings,
children,
resolver,
}: React.PropsWithChildren<{
settings: InitOptions;
resolver: Resolver;
}>) {
// If the client is not initialized or
// the language has changed, reinitialize the client
if (!client || client.language !== settings.lng) {
throw withI18nClient(settings, resolver);
}
return children;
}
async function withI18nClient(settings: InitOptions, resolver: Resolver) {
if (typeof window !== 'undefined') {
const { initializeI18nClient } = await import('./i18n.client');
client = await initializeI18nClient(settings, resolver);
} else {
const { initializeServerI18n } = await import('./i18n.server');
client = await initializeServerI18n(settings, resolver);
}
}