feat: wire i18n services and shared UI

This commit is contained in:
poyrazavsever
2026-07-19 03:06:56 +03:00
parent 805beccf95
commit 826410f53e
11 changed files with 1330 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
"use client";
import {
createContext,
useContext,
useMemo,
type ReactNode,
} from "react";
import {
createTranslatorFromMessages,
type Translator,
type TranslationValues,
} from "@/lib/i18n";
type I18nContextValue = Translator;
const I18nContext = createContext<I18nContextValue | null>(null);
export function I18nProvider({
children,
locale,
messages,
}: {
children: ReactNode;
locale: string;
messages: Record<string, string>;
}) {
const value = useMemo(
() => createTranslatorFromMessages(locale, messages),
[locale, messages],
);
return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;
}
export function useTranslations() {
const value = useContext(I18nContext);
if (!value) {
throw new Error("useTranslations must be used within I18nProvider.");
}
return (key: string, values?: TranslationValues) => value.t(key, values);
}
export function useI18n() {
const value = useContext(I18nContext);
if (!value) {
throw new Error("useI18n must be used within I18nProvider.");
}
return value;
}