Files
calendar/apps/web/app/AppRouterI18nProvider.tsx
T
Benny JooandGitHub b17b5a169f perf: make useLocale hook to use server-fetched i18n if in App router context (#20597)
* write I18nProvider

* rename

* update useLocale

* fix

* use a singleton

* safer

* better

* fix e2e test 1

* fix e2e test 2

* fix e2e test 3

* fix e2e test 4

* fix

* refactor

* address comments

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix flaky test

* add more caching

* revert changes to e2e tests

* add jsdoc

* memoize

* update window object with new translations in /settings/my-account/general

* remove dead code

* only use locale and ns as deps

* fix
2025-04-11 14:28:26 -07:00

34 lines
731 B
TypeScript

"use client";
import { createContext, useMemo } from "react";
import type { ReactNode } from "react";
type AppRouterI18nContextType = {
translations: Record<string, string>;
ns: string;
locale: string;
};
export const AppRouterI18nContext = createContext<AppRouterI18nContextType | null>(null);
export function AppRouterI18nProvider({
children,
translations,
locale,
ns,
}: AppRouterI18nContextType & {
children: ReactNode;
}) {
// Memoize the value to prevent re-renders unless the data changes
const value = useMemo(
() => ({
translations,
locale,
ns,
}),
[locale, ns]
);
return <AppRouterI18nContext.Provider value={value}>{children}</AppRouterI18nContext.Provider>;
}