Add central exercise translation system (7 languages, 30+ keys)
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>
This commit is contained in:
38
studio-v2/lib/useNativeLanguage.ts
Normal file
38
studio-v2/lib/useNativeLanguage.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
'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 }
|
||||
}
|
||||
Reference in New Issue
Block a user