'use client' import { useState, useEffect } from 'react' import { exerciseT, type ExerciseKey } from './exerciseTranslations' const STORAGE_KEY = 'bp_native_language' /** * Hook to read the user's native language. * Returns translation helper for exercise UI texts. */ export function useNativeLanguage() { const [nativeLang, setNativeLang] = useState('de') useEffect(() => { const stored = localStorage.getItem(STORAGE_KEY) if (stored) setNativeLang(stored) }, []) const isThirdLanguage = nativeLang !== 'de' && nativeLang !== 'en' /** Get translated exercise UI text */ const t = (key: ExerciseKey): string => { const entry = exerciseT[key] if (!entry) return key return (entry as Record)[nativeLang] || entry.de || key } /** Get native translation of a vocab word from translations JSONB */ const wordInNative = (translations?: Record): string => { if (!translations || !isThirdLanguage) return '' const entry = translations[nativeLang] if (!entry) return '' return typeof entry === 'string' ? entry : entry.text || '' } return { nativeLang, isThirdLanguage, t, wordInNative } }