// The editor's visible texts, German and English (plan §7.5): English keys, // the words in i18n/{de,en}.json — where Java FILIUS has a word for a thing, // Java's (MessagesBundle), so that the terms on worksheets fit. The language // comes from the dashboard over the bridge. import { createContext, useContext, type ReactNode } from 'react'; import de from './de.json'; import en from './en.json'; export type Language = 'de' | 'en'; export const CATALOGS: Readonly>>> = { de, en }; export type Translate = (key: string, values?: Readonly>) => string; /** The text of a key in a language, its {placeholders} filled; the key itself where the catalog has none. */ export function translate(language: Language, key: string, values: Readonly> = {}): string { const text = CATALOGS[language][key] ?? key; return text.replace(/\{(\w+)\}/g, (whole, name: string) => (name in values ? String(values[name]) : whole)); } const LanguageContext = createContext('de'); export function LanguageProvider({ language, children }: { readonly language: Language; readonly children: ReactNode }) { return {children}; } export function useLanguage(): Language { return useContext(LanguageContext); } export function useTranslate(): Translate { const language = useContext(LanguageContext); return (key, values) => translate(language, key, values); }