Files
breakpilot-lehrer/studio-v2/lib/useNativeLanguage.ts
Benjamin Admin 5012699aaf Add persistent language switcher across all learn/parent pages
useNativeLanguage: Now has setNativeLang() that persists to localStorage.
Language selection carries across all pages automatically.

LanguageSwitcher: Compact dropdown component added to learn/layout.tsx
and parent/layout.tsx — visible on every sub-page (top-right).

Parent portal: Language dropdown syncs both UI language and native
language. Parents can switch language mid-session (e.g. when both
parents speak different languages).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-27 09:23:01 +02:00

45 lines
1.4 KiB
TypeScript

'use client'
import { useState, useEffect, useCallback } from 'react'
import { exerciseT, type ExerciseKey } from './exerciseTranslations'
const STORAGE_KEY = 'bp_native_language'
/**
* Hook for native language state + translations.
* Persists to localStorage. Can be changed at any time (e.g. parent switches language).
*/
export function useNativeLanguage() {
const [nativeLang, setNativeLangState] = useState('de')
useEffect(() => {
const stored = localStorage.getItem(STORAGE_KEY)
if (stored) setNativeLangState(stored)
}, [])
/** Change native language (persists to localStorage) */
const setNativeLang = useCallback((lang: string) => {
setNativeLangState(lang)
localStorage.setItem(STORAGE_KEY, lang)
}, [])
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<string, string>)[nativeLang] || entry.de || key
}
/** Get native translation of a vocab word from translations JSONB */
const wordInNative = (translations?: Record<string, any>): string => {
if (!translations || !isThirdLanguage) return ''
const entry = translations[nativeLang]
if (!entry) return ''
return typeof entry === 'string' ? entry : entry.text || ''
}
return { nativeLang, setNativeLang, isThirdLanguage, t, wordInNative }
}