import enMessages from "@/messages/en.json"; import trMessages from "@/messages/tr.json"; import { create } from "zustand"; export type Locale = "tr" | "en"; const messages: Record> = { tr: trMessages, en: enMessages, }; interface I18nState { locale: Locale; setLocale: (locale: Locale) => void; } export const useI18nStore = create((set) => ({ locale: "tr", setLocale: (locale) => { set({ locale }); if (typeof window !== "undefined") { localStorage.setItem("sase-locale", locale); document.documentElement.lang = locale; } }, })); export function initLocale(): void { if (typeof window !== "undefined") { const saved = localStorage.getItem("sase-locale") as Locale | null; if (saved && (saved === "tr" || saved === "en")) { useI18nStore.getState().setLocale(saved); } } } function getNestedValue(obj: unknown, path: string): string { const keys = path.split("."); let current: unknown = obj; for (const key of keys) { if (current === null || current === undefined || typeof current !== "object") { return path; } current = (current as Record)[key]; } if (typeof current === "string") { return current; } return path; } export function t(key: string): string { const locale = useI18nStore.getState().locale; return getNestedValue(messages[locale], key); } export function useTranslation() { const locale = useI18nStore((state) => state.locale); const setLocale = useI18nStore((state) => state.setLocale); const translate = (key: string): string => { return getNestedValue(messages[locale], key); }; return { t: translate, locale, setLocale }; }