'use client' import { createContext, useContext, useState, useEffect, ReactNode } from 'react' import { Lang, translations } from '@/lib/i18n' interface LangCtx { lang: Lang setLang: (l: Lang) => void t: typeof translations.de } const LangContext = createContext({ lang: 'de', setLang: () => {}, t: translations.de, }) function getInitialLang(): Lang { if (typeof document === 'undefined') return 'de' const match = document.cookie.match(/(?:^|;\s*)lang=([^;]+)/) const val = match?.[1] return val === 'en' || val === 'es' || val === 'de' ? val : 'de' } export function LangProvider({ children }: { children: ReactNode }) { const [lang, setLangState] = useState('de') useEffect(() => { setLangState(getInitialLang()) }, []) const setLang = (l: Lang) => { document.cookie = `lang=${l};path=/;max-age=31536000;SameSite=Lax` // Update for the current page visit without a full reload document.documentElement.lang = l setLangState(l) } return ( {children} ) } export const useLang = () => useContext(LangContext)