useNativeLanguage.ts: Hook that reads bp_native_language from localStorage and provides t(key) for translated UI text and wordInNative() for vocabulary translations. exerciseTranslations.ts: All exercise UI strings in DE/EN/TR/AR/UK/RU/PL. Buttons (Richtig/Falsch), instructions, labels, result texts. Next: Wire into all 9 exercise pages for trilingual display. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
'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<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, isThirdLanguage, t, wordInNative }
|
|
}
|