46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
"use client";
|
|
|
|
import { ReactNode, createContext, useContext } from "react";
|
|
|
|
import {
|
|
DEFAULT_LOCALE,
|
|
I18nKey,
|
|
Locale,
|
|
TranslationParams,
|
|
translateText,
|
|
} from "@/shared/config/i18n";
|
|
|
|
type I18nContextValue = {
|
|
locale: Locale;
|
|
setLocale: (locale: Locale) => void;
|
|
t: (key: I18nKey | string, params?: TranslationParams, fallback?: string) => string;
|
|
};
|
|
|
|
const I18nContext = createContext<I18nContextValue>({
|
|
locale: DEFAULT_LOCALE,
|
|
setLocale: () => {},
|
|
t: (key, params, fallback) =>
|
|
translateText(DEFAULT_LOCALE, key, params, fallback),
|
|
});
|
|
|
|
export function I18nProvider({
|
|
children,
|
|
locale,
|
|
setLocale,
|
|
}: {
|
|
children: ReactNode;
|
|
locale: Locale;
|
|
setLocale: (locale: Locale) => void;
|
|
}) {
|
|
const t = (key: I18nKey | string, params?: TranslationParams, fallback?: string) =>
|
|
translateText(locale, key, params, fallback);
|
|
|
|
return (
|
|
<I18nContext.Provider value={{ locale, setLocale, t }}>
|
|
{children}
|
|
</I18nContext.Provider>
|
|
);
|
|
}
|
|
|
|
export const useI18n = () => useContext(I18nContext);
|