Wörterbücher unter lib/i18n statt next-intl (Next.js 16 ist neu genug, dass eine externe Routing-Middleware zusätzliches Risiko bedeutet hätte); Sprache liegt in einem Cookie, kein URL-Präfix nötig, dadurch bleibt proxy.ts unverändert. LocaleProvider stellt Wörterbuch + Sprache per Context für Client-Komponenten bereit (lib/i18n/shared.ts ist bewusst getrennt von lib/i18n/index.ts, damit next/headers nicht in den Client-Bundle gelangt).
27 lines
840 B
TypeScript
27 lines
840 B
TypeScript
// Client-sicherer Teil von lib/i18n: keine Abhängigkeit auf next/headers,
|
|
// darf daher auch von "use client"-Komponenten importiert werden.
|
|
|
|
import { dictionaries, type Dict } from "./dictionaries";
|
|
import type { Locale } from "./locales";
|
|
|
|
export { LOCALES, LOCALE_LABELS, DEFAULT_LOCALE, isLocale } from "./locales";
|
|
export type { Locale } from "./locales";
|
|
export type { Dict } from "./dictionaries";
|
|
|
|
export const LOCALE_COOKIE = "locale";
|
|
|
|
export function getDictionary(locale: Locale): Dict {
|
|
return dictionaries[locale];
|
|
}
|
|
|
|
/** Platzhalter der Form "{name}" durch vars.name ersetzen. */
|
|
export function formatMessage(
|
|
template: string,
|
|
vars?: Record<string, string | number>
|
|
): string {
|
|
if (!vars) return template;
|
|
return template.replace(/\{(\w+)\}/g, (match, key) =>
|
|
key in vars ? String(vars[key]) : match
|
|
);
|
|
}
|