35 lines
854 B
TypeScript
35 lines
854 B
TypeScript
"use client";
|
|
|
|
import { createContext, ReactNode, useContext } from "react";
|
|
import type { Dictionary } from "./types";
|
|
import type { Locale } from "./config";
|
|
|
|
type I18nContextValue = {
|
|
locale: Locale;
|
|
dictionary: Dictionary;
|
|
};
|
|
|
|
const I18nContext = createContext<I18nContextValue | undefined>(undefined);
|
|
|
|
export function I18nProvider({ value, children }: { value: I18nContextValue; children: ReactNode }) {
|
|
return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;
|
|
}
|
|
|
|
export function useI18n() {
|
|
const context = useContext(I18nContext);
|
|
if (!context) {
|
|
throw new Error("useI18n must be used within an I18nProvider");
|
|
}
|
|
return context;
|
|
}
|
|
|
|
export function useDictionary() {
|
|
const { dictionary } = useI18n();
|
|
return dictionary;
|
|
}
|
|
|
|
export function useLocale() {
|
|
const { locale } = useI18n();
|
|
return locale;
|
|
}
|