"use client"; import { createContext, useContext, useState, useCallback } from "react"; import { en } from "./en"; import { zh } from "./zh"; import type { LandingDict, Locale } from "./types"; const dictionaries: Record = { en, zh }; const COOKIE_NAME = "multica-locale"; const COOKIE_MAX_AGE = 60 * 60 * 24 * 365; // 1 year type LocaleContextValue = { locale: Locale; t: LandingDict; setLocale: (locale: Locale) => void; }; const LocaleContext = createContext(null); export function LocaleProvider({ children, initialLocale = "en", }: { children: React.ReactNode; initialLocale?: Locale; }) { const [locale, setLocaleState] = useState(initialLocale); const setLocale = useCallback((l: Locale) => { setLocaleState(l); document.cookie = `${COOKIE_NAME}=${l}; path=/; max-age=${COOKIE_MAX_AGE}; SameSite=Lax`; }, []); return ( {children} ); } export function useLocale() { const ctx = useContext(LocaleContext); if (!ctx) throw new Error("useLocale must be used within LocaleProvider"); return ctx; }