[129][117] Merge: Kern ohne React (i18n/translate), Grenztest transitiv + net/; Material Symbols lokal statt Google Fonts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
18 files changed
+153
-110
No files matched your search
@@ -1,5 +1,9 @@
|
||||
Dossier — Hinweise zu übernommenem und eingebundenem Fremdcode ([51])
|
||||
|
||||
Material Symbols (Icon-Font, npm-Paket material-symbols, https://github.com/marella/material-symbols; Schrift von Google,
|
||||
https://github.com/google/material-design-icons), Apache-2.0
|
||||
Lokal gebündelt (src/main.tsx), statt von fonts.googleapis.com geladen ([117]).
|
||||
|
||||
acadrust (https://github.com/hakanaktt/acadrust), MPL-2.0
|
||||
Abhängigkeit von src-tauri/fileio (DXF/DWG lesen und schreiben), unverändert eingebunden.
|
||||
Der DWG-Code von acadrust ist aus ACadSharp portiert:
|
||||
|
||||
@@ -1048,7 +1048,7 @@ Prüfung des ganzen Programms (Rust-Kern, TS-Hülle, Build/Sicherheit), nur Code
|
||||
(Exporte/`saveBinaryFile` schreiben weiter direkt); in der Tauri-App nicht von Hand geprüft; Electron
|
||||
schreibt keine Dateien (Browser-Download), daher keine Änderung; keine Wiederherstellungs-UI für `.obp~`. · S
|
||||
- ✅ **[117] CSP setzen.** (3.11.) Eine Quelle `scripts/csp.cjs` (`script-src 'self' 'wasm-unsafe-eval'`, `worker-src 'self' blob:`,
|
||||
`style-src` mit `'unsafe-inline'` für React-Inline-Styles + fonts.googleapis.com, `connect-src` = ipc + netFetch-Hosts +
|
||||
`style-src` mit `'unsafe-inline'` für React-Inline-Styles (Icon-Font seit 3.11. lokal gebündelt: `material-symbols`, kein Google-Fonts-Host mehr), `connect-src` = ipc + netFetch-Hosts +
|
||||
localhost/127.0.0.1 + ws://127.0.0.1 für die KI-Brücke). Tauri: `tauri.conf.json` (`devCsp: null`, Test `src/net/csp.test.ts`
|
||||
gleicht mit `csp.cjs` und `ALLOWED_HOSTS` ab); Electron: Antwort-Header nur gepackt; Browser/e2e: Meta-Tag nur im Vite-Build
|
||||
(Dev-HMR unberührt). `e2e/csp.spec.ts` prüft Plan/Schnitt/Ansicht/3D auf CSP-Verletzungen. Rich-Text: Text war schon
|
||||
@@ -1075,8 +1075,9 @@ Prüfung des ganzen Programms (Rust-Kern, TS-Hülle, Build/Sicherheit), nur Code
|
||||
- ○ **[127] 3D-Booleans klären:** BSP in `fileio/csg.rs` stilllegen, Manifold vs. truck dokumentieren. · S
|
||||
- ○ **[128] Restlogik nach Rust:** Griffe (`useGripEditing.ts`), `layoutModel.ts`, Kollisions-Vorprüfung,
|
||||
`exportStep.projectSolidSpecs`. · L
|
||||
- ○ **[129] Grenze schärfen:** React-freies `t()` (Kern importiert `i18n` → React), `net/` in `CORE_DIRS`,
|
||||
transitive Importe prüfen. · S
|
||||
- ✅ **[129] Grenze schärfen.** (3.11.) `i18n/translate.ts` (React-frei: `t`, Sprache, Wörterbücher), `i18n/index.ts` re-exportiert
|
||||
und hält nur `useT`; Kern-Dateien importieren `i18n/translate`. `net/` in `CORE_DIRS`, neuer Test prüft transitive
|
||||
relative Importe (Ausnahmelisten leer). · S
|
||||
- ○ **[130] Kleinkram:** eine generische Fenster-Brücke statt 5 Kopien; Auswahl als ein `Selection`-Objekt statt
|
||||
12 Felder; `primitives.tsx` auf Overlay zurückschneiden; Cargo-Workspace + cargo-deny; NOTICE (OFL-Schriften,
|
||||
wgpu, Tauri, React, pdf.js, lcms). · M
|
||||
|
||||
+1
-8
@@ -3,14 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<!-- Material Symbols (Icon-Font) — DOSSIER-Look; ohne diese Font erschienen
|
||||
die Ligaturnamen (z. B. „content_copy") als roher Text in Menüs/Toolbar. -->
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200"
|
||||
/>
|
||||
<!-- Material Symbols (Icon-Font) wird lokal über src/main.tsx gebündelt ([117]). -->
|
||||
<link
|
||||
rel="icon"
|
||||
href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Crect width='16' height='16' rx='3' fill='%232f6df6'/%3E%3Cg fill='white'%3E%3Crect x='3' y='3' width='4' height='4'/%3E%3Crect x='9' y='3' width='4' height='4'/%3E%3Crect x='3' y='9' width='4' height='4'/%3E%3Crect x='9' y='9' width='4' height='4'/%3E%3C/g%3E%3C/svg%3E"
|
||||
|
||||
Generated
+7
@@ -19,6 +19,7 @@
|
||||
"jspdf": "^4.2.1",
|
||||
"jszip": "^3.10.1",
|
||||
"lcms-wasm": "^1.0.5",
|
||||
"material-symbols": "^0.48.2",
|
||||
"pdfjs-dist": "^6.2.108",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
@@ -4562,6 +4563,12 @@
|
||||
"@jridgewell/sourcemap-codec": "^1.5.5"
|
||||
}
|
||||
},
|
||||
"node_modules/material-symbols": {
|
||||
"version": "0.48.2",
|
||||
"resolved": "https://registry.npmjs.org/material-symbols/-/material-symbols-0.48.2.tgz",
|
||||
"integrity": "sha512-HepjQnrny9CsxBuTofUJsHS1Il41l3oa9ZiLHTnrKJKZbIyDvXHZJBD1ffLEO/5lSKdb8v2filRW59IPsO65yA==",
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/math-intrinsics": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
"jspdf": "^4.2.1",
|
||||
"jszip": "^3.10.1",
|
||||
"lcms-wasm": "^1.0.5",
|
||||
"material-symbols": "^0.48.2",
|
||||
"pdfjs-dist": "^6.2.108",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
|
||||
+2
-5
@@ -15,9 +15,6 @@ const NET_HOSTS = [
|
||||
];
|
||||
|
||||
const https = NET_HOSTS.map((h) => `https://${h}`);
|
||||
// Icon-Font (index.html): Stylesheet von fonts.googleapis.com, Schrift von fonts.gstatic.com.
|
||||
const GOOGLE_FONTS_CSS = "https://fonts.googleapis.com";
|
||||
const GOOGLE_FONTS_FILES = "https://fonts.gstatic.com";
|
||||
|
||||
const DIRECTIVES = {
|
||||
"default-src": ["'self'"],
|
||||
@@ -26,8 +23,8 @@ const DIRECTIVES = {
|
||||
// Import-Worker (DXF/IFC/Mesh/Punktwolke) und pdf.js-Worker: Modul-URLs aus dem eigenen Bundle.
|
||||
"worker-src": ["'self'", "blob:"],
|
||||
// React setzt viele Inline-`style`-Attribute -> 'unsafe-inline' (nur Stile, keine Skripte).
|
||||
"style-src": ["'self'", "'unsafe-inline'", GOOGLE_FONTS_CSS],
|
||||
"font-src": ["'self'", "data:", GOOGLE_FONTS_FILES],
|
||||
"style-src": ["'self'", "'unsafe-inline'"],
|
||||
"font-src": ["'self'", "data:"],
|
||||
"img-src": ["'self'", "data:", "blob:", ...https],
|
||||
"media-src": ["'self'", "blob:"],
|
||||
"connect-src": [
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' data: https://fonts.gstatic.com; img-src 'self' data: blob: https://ambientcg.com https://api.ambientcg.com https://geo.admin.ch https://api3.geo.admin.ch https://wms.geo.admin.ch https://data.geo.admin.ch https://overpass-api.de https://nominatim.openstreetmap.org; media-src 'self' blob:; connect-src 'self' ipc: http://ipc.localhost blob: data: https://ambientcg.com https://api.ambientcg.com https://geo.admin.ch https://api3.geo.admin.ch https://wms.geo.admin.ch https://data.geo.admin.ch https://overpass-api.de https://nominatim.openstreetmap.org http://localhost:* http://127.0.0.1:* ws://127.0.0.1:*; object-src 'none'; base-uri 'self'; form-action 'none'",
|
||||
"csp": "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; img-src 'self' data: blob: https://ambientcg.com https://api.ambientcg.com https://geo.admin.ch https://api3.geo.admin.ch https://wms.geo.admin.ch https://data.geo.admin.ch https://overpass-api.de https://nominatim.openstreetmap.org; media-src 'self' blob:; connect-src 'self' ipc: http://ipc.localhost blob: data: https://ambientcg.com https://api.ambientcg.com https://geo.admin.ch https://api3.geo.admin.ch https://wms.geo.admin.ch https://data.geo.admin.ch https://overpass-api.de https://nominatim.openstreetmap.org http://localhost:* http://127.0.0.1:* ws://127.0.0.1:*; object-src 'none'; base-uri 'self'; form-action 'none'",
|
||||
"devCsp": null
|
||||
}
|
||||
},
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
// Der Aufruf gehört in EIN `setProject` (ein Undo-Schritt).
|
||||
|
||||
import type { Drawing2D, DrawingLevel, Project } from "../model/types";
|
||||
import { t } from "../i18n";
|
||||
import { t } from "../i18n/translate";
|
||||
import { applyOps } from "../core/ops";
|
||||
import type { CoreOp } from "../core/ops";
|
||||
import { addItems } from "../core/opsBuild";
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// Wächter für die Kern/Hülle-Grenze (docs/core-ui-split.md, Phase A). Es gibt kein ESLint im
|
||||
// Projekt — die Regeln laufen deshalb als Test über die Importzeilen aller Quelldateien.
|
||||
//
|
||||
// 1. Der KERN (model, geometry, plan, io, export, analysis, materials, text, engine) importiert
|
||||
// 1. Der KERN (core, model, geometry, plan, io, export, analysis, materials, text, engine, net) — auch
|
||||
// transitiv über relative Importe importiert
|
||||
// nie die HÜLLE (ui, panels, views, menus, editors, viewport, native, tools, state, commands,
|
||||
// App) und nie React/Zustand/Tauri statisch.
|
||||
// 2. Die HÜLLE holt Funktionen aus `plan/` und `geometry/` nur über die Fassade `src/core`.
|
||||
@@ -14,7 +15,7 @@ import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { dirname, join, normalize, relative, sep } from "node:path";
|
||||
|
||||
const SRC = join(process.cwd(), "src");
|
||||
const CORE_DIRS = new Set(["core", "model", "geometry", "plan", "io", "export", "analysis", "materials", "text", "engine"]);
|
||||
const CORE_DIRS = new Set(["core", "model", "geometry", "plan", "io", "export", "analysis", "materials", "text", "engine", "net"]);
|
||||
const SHELL_DIRS = new Set(["ui", "panels", "views", "menus", "editors", "viewport", "native", "tools", "state", "commands"]);
|
||||
const FACADE_DIRS = new Set(["plan", "geometry"]);
|
||||
const FORBIDDEN_PACKAGES = /^(react|react-dom|zustand|@tauri-apps\/.*)$/;
|
||||
@@ -80,6 +81,43 @@ describe("Kern/Hülle-Grenze", () => {
|
||||
expect(violations).toEqual([]);
|
||||
});
|
||||
|
||||
it("der Kern erreicht transitiv keine Hülle und keine UI-Pakete", () => {
|
||||
const violations: string[] = [];
|
||||
const resolve = (target: string): string | null => {
|
||||
for (const c of [`${target}.ts`, `${target}.tsx`, `${target}/index.ts`, `${target}/index.tsx`, target]) {
|
||||
const abs = join(SRC, c);
|
||||
try {
|
||||
if (statSync(abs).isFile()) return abs;
|
||||
} catch {
|
||||
/* nächster Kandidat */
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
for (const root of files) {
|
||||
if (!CORE_DIRS.has(top(root))) continue;
|
||||
const seen = new Set<string>([root]);
|
||||
const queue: Array<{ file: string; path: string[] }> = [{ file: root, path: [] }];
|
||||
while (queue.length) {
|
||||
const { file, path } = queue.shift()!;
|
||||
for (const i of importsOf(file)) {
|
||||
const tt = i.target?.split("/")[0];
|
||||
const chain = () => [relative(SRC, root), ...path, i.target ?? i.spec].join(" → ");
|
||||
if (tt && (SHELL_DIRS.has(tt) || tt === "App" || tt === "main")) violations.push(chain());
|
||||
else if (!i.target && FORBIDDEN_PACKAGES.test(i.spec)) violations.push(chain());
|
||||
else if (i.target) {
|
||||
const next = resolve(i.target);
|
||||
if (next && !seen.has(next) && !/\.test\.tsx?$/.test(next)) {
|
||||
seen.add(next);
|
||||
queue.push({ file: next, path: [...path, i.target] });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
expect([...new Set(violations)]).toEqual([]);
|
||||
});
|
||||
|
||||
it("die Hülle holt plan/ und geometry/ nur über die Fassade src/core", () => {
|
||||
const violations: string[] = [];
|
||||
for (const f of files) {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
import { jsPDF } from "jspdf";
|
||||
import type { Project } from "../model/types";
|
||||
import { t } from "../i18n";
|
||||
import { t } from "../i18n/translate";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { computeAreaTable, computeIvhb } from "../analysis/areaTable";
|
||||
import type { AreaRow, IvhbOptions } from "../analysis/areaTable";
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// (wie die Fenster-/Türliste, `openingSchedule.ts`).
|
||||
|
||||
import type { Layer, Project } from "../model/types";
|
||||
import { t } from "../i18n";
|
||||
import { t } from "../i18n/translate";
|
||||
import { documentFor } from "../core/document";
|
||||
import type { AreaReport, Cell, ReportRow } from "./areaReport";
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
import { jsPDF } from "jspdf";
|
||||
import "svg2pdf.js";
|
||||
import type { Layout, LayoutPaperFormat, MasterLayout, Project } from "../model/types";
|
||||
import { t } from "../i18n";
|
||||
import { t } from "../i18n/translate";
|
||||
import { legendPlan, legendPrintSvg } from "./legendSvg";
|
||||
import { categoryLayerGroups, enablePdfLayers, type PdfLayers } from "./pdfLayers";
|
||||
import { generatePlan, type Plan } from "../plan/generatePlan";
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// (`areaReport.ts`: `areaReportToCsv` / `areaReportToSvg` / `buildAreaReportPdf`).
|
||||
|
||||
import type { Project } from "../model/types";
|
||||
import { t } from "../i18n";
|
||||
import { t } from "../i18n/translate";
|
||||
import { documentFor } from "../core/document";
|
||||
import type { AreaReport, Cell, ReportRow, ReportSection } from "./areaReport";
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// Die Zahlen rechnet der Kern (`analysis/roomProgram.ts`); hier nur die Beschriftung mit `t()`.
|
||||
|
||||
import type { Project } from "../model/types";
|
||||
import { t } from "../i18n";
|
||||
import { t } from "../i18n/translate";
|
||||
import { compareRoomProgram } from "../analysis/roomProgram";
|
||||
import type { CompareSum, Light } from "../analysis/roomProgram";
|
||||
import type { AreaReport, Cell, ReportRow, ReportSection } from "./areaReport";
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type { jsPDF } from "jspdf";
|
||||
import type { Layout, LayoutTable, LayoutTableSource, Project } from "../model/types";
|
||||
import { t } from "../i18n";
|
||||
import { t } from "../i18n/translate";
|
||||
import { documentFor } from "../core/document";
|
||||
|
||||
export type { LayoutTable, LayoutTableSource };
|
||||
|
||||
+5
-84
@@ -1,94 +1,15 @@
|
||||
// i18n-Schicht — eine einzige Übersetzungsfunktion `t(key, params?)` plus ein
|
||||
// optionaler React-Hook `useT`. Default-Sprache ist Deutsch; das deutsche
|
||||
// Wörterbuch (de.ts) hält die EXAKTEN bisherigen UI-Texte, sodass die Umstellung
|
||||
// optisch nichts ändert. en.ts beweist die Übersetzbarkeit.
|
||||
//
|
||||
// Bewusst minimal gehalten: ein modulweiter „current language" + Wörterbuch-
|
||||
// Lookup mit {param}-Interpolation. Kein Provider-Zwang — t(...) ist überall
|
||||
// direkt aufrufbar; useT() liefert dieselbe Funktion und re-rendert Komponenten
|
||||
// beim Sprachwechsel (subscribe), falls je ein Umschalter gebaut wird.
|
||||
//
|
||||
// Identifier/Keys bleiben englisch (DOSSIER-Namespacing); nur die Wörterbuch-
|
||||
// Werte sind übersetzbarer Text.
|
||||
// i18n-Fassade: React-freier Kern in ./translate (für Kern-Module), dazu der React-Hook `useT`.
|
||||
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { de } from "./de";
|
||||
import { en } from "./en";
|
||||
import type { TranslationKey } from "./de";
|
||||
import { getLanguage, subscribeLanguage, t } from "./translate";
|
||||
|
||||
export type { TranslationKey } from "./de";
|
||||
|
||||
/** Unterstützte Sprachen. Default ist „de". */
|
||||
export type Language = "de" | "en";
|
||||
|
||||
/** Wörterbücher je Sprache. de ist die Quelle der Keys (Default-Sprache). */
|
||||
const DICTS: Record<Language, Record<TranslationKey, string>> = { de, en };
|
||||
|
||||
/** Werte für die Platzhalter-Interpolation (`{name}` → params.name). */
|
||||
export type TParams = Record<string, string | number>;
|
||||
|
||||
// ── Modulweiter Sprachzustand + Abonnenten (für useT) ──────────────────────
|
||||
|
||||
let currentLanguage: Language = "de";
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
/** Aktuell gewählte Sprache. */
|
||||
export function getLanguage(): Language {
|
||||
return currentLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprache umschalten. Benachrichtigt abonnierte Komponenten (useT), damit sie
|
||||
* mit den neuen Texten neu rendern. (Noch kein UI-Umschalter — die Naht ist
|
||||
* aber vorhanden und verdrahtet.)
|
||||
*/
|
||||
export function setLanguage(lang: Language): void {
|
||||
if (lang === currentLanguage) return;
|
||||
currentLanguage = lang;
|
||||
for (const l of listeners) l();
|
||||
}
|
||||
|
||||
// ── Kern: Lookup + Interpolation ───────────────────────────────────────────
|
||||
|
||||
/** Ersetzt `{name}`-Platzhalter im Wert durch die übergebenen Parameter. */
|
||||
function interpolate(value: string, params?: TParams): string {
|
||||
if (!params) return value;
|
||||
return value.replace(/\{(\w+)\}/g, (match, name: string) =>
|
||||
name in params ? String(params[name]) : match,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Übersetzt einen Key in der aktuellen Sprache mit optionaler {param}-
|
||||
* Interpolation. Fehlt der Key in der gewählten Sprache, greift Deutsch als
|
||||
* Fallback; fehlt er auch dort, wird der Key selbst zurückgegeben (sichtbar,
|
||||
* damit fehlende Texte auffallen statt leer zu bleiben).
|
||||
*/
|
||||
export function t(key: TranslationKey | string, params?: TParams): string {
|
||||
const dict = DICTS[currentLanguage];
|
||||
const value =
|
||||
(dict as Record<string, string>)[key] ??
|
||||
(de as Record<string, string>)[key] ??
|
||||
key;
|
||||
return interpolate(value, params);
|
||||
}
|
||||
|
||||
// ── React-Hook ─────────────────────────────────────────────────────────────
|
||||
export * from "./translate";
|
||||
|
||||
/**
|
||||
* Hook-Variante: liefert dieselbe `t`-Funktion, abonniert aber den
|
||||
* Sprachzustand, sodass die Komponente beim Sprachwechsel neu rendert. In den
|
||||
* meisten Komponenten reicht der direkte `t`-Import; useT() ist für den Fall
|
||||
* gedacht, dass ein Live-Sprachumschalter existiert.
|
||||
* Sprachzustand, sodass die Komponente beim Sprachwechsel neu rendert.
|
||||
*/
|
||||
export function useT(): typeof t {
|
||||
useSyncExternalStore(
|
||||
(cb) => {
|
||||
listeners.add(cb);
|
||||
return () => listeners.delete(cb);
|
||||
},
|
||||
getLanguage,
|
||||
getLanguage,
|
||||
);
|
||||
useSyncExternalStore(subscribeLanguage, getLanguage, getLanguage);
|
||||
return t;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// i18n-Kern (React-frei) — von Kern-Modulen importierbar. Der Hook `useT` liegt in index.ts.
|
||||
// i18n-Schicht — eine einzige Übersetzungsfunktion `t(key, params?)` plus ein
|
||||
// optionaler React-Hook `useT`. Default-Sprache ist Deutsch; das deutsche
|
||||
// Wörterbuch (de.ts) hält die EXAKTEN bisherigen UI-Texte, sodass die Umstellung
|
||||
// optisch nichts ändert. en.ts beweist die Übersetzbarkeit.
|
||||
//
|
||||
// Bewusst minimal gehalten: ein modulweiter „current language" + Wörterbuch-
|
||||
// Lookup mit {param}-Interpolation. Kein Provider-Zwang — t(...) ist überall
|
||||
// direkt aufrufbar; useT() liefert dieselbe Funktion und re-rendert Komponenten
|
||||
// beim Sprachwechsel (subscribe), falls je ein Umschalter gebaut wird.
|
||||
//
|
||||
// Identifier/Keys bleiben englisch (DOSSIER-Namespacing); nur die Wörterbuch-
|
||||
// Werte sind übersetzbarer Text.
|
||||
|
||||
import { de } from "./de";
|
||||
import { en } from "./en";
|
||||
import type { TranslationKey } from "./de";
|
||||
|
||||
export type { TranslationKey } from "./de";
|
||||
|
||||
/** Unterstützte Sprachen. Default ist „de". */
|
||||
export type Language = "de" | "en";
|
||||
|
||||
/** Wörterbücher je Sprache. de ist die Quelle der Keys (Default-Sprache). */
|
||||
const DICTS: Record<Language, Record<TranslationKey, string>> = { de, en };
|
||||
|
||||
/** Werte für die Platzhalter-Interpolation (`{name}` → params.name). */
|
||||
export type TParams = Record<string, string | number>;
|
||||
|
||||
// ── Modulweiter Sprachzustand + Abonnenten (für useT) ──────────────────────
|
||||
|
||||
let currentLanguage: Language = "de";
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
/** Aktuell gewählte Sprache. */
|
||||
export function getLanguage(): Language {
|
||||
return currentLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprache umschalten. Benachrichtigt abonnierte Komponenten (useT), damit sie
|
||||
* mit den neuen Texten neu rendern. (Noch kein UI-Umschalter — die Naht ist
|
||||
* aber vorhanden und verdrahtet.)
|
||||
*/
|
||||
export function setLanguage(lang: Language): void {
|
||||
if (lang === currentLanguage) return;
|
||||
currentLanguage = lang;
|
||||
for (const l of listeners) l();
|
||||
}
|
||||
|
||||
// ── Kern: Lookup + Interpolation ───────────────────────────────────────────
|
||||
|
||||
/** Ersetzt `{name}`-Platzhalter im Wert durch die übergebenen Parameter. */
|
||||
function interpolate(value: string, params?: TParams): string {
|
||||
if (!params) return value;
|
||||
return value.replace(/\{(\w+)\}/g, (match, name: string) =>
|
||||
name in params ? String(params[name]) : match,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Übersetzt einen Key in der aktuellen Sprache mit optionaler {param}-
|
||||
* Interpolation. Fehlt der Key in der gewählten Sprache, greift Deutsch als
|
||||
* Fallback; fehlt er auch dort, wird der Key selbst zurückgegeben (sichtbar,
|
||||
* damit fehlende Texte auffallen statt leer zu bleiben).
|
||||
*/
|
||||
export function t(key: TranslationKey | string, params?: TParams): string {
|
||||
const dict = DICTS[currentLanguage];
|
||||
const value =
|
||||
(dict as Record<string, string>)[key] ??
|
||||
(de as Record<string, string>)[key] ??
|
||||
key;
|
||||
return interpolate(value, params);
|
||||
}
|
||||
|
||||
/** Abo für den Sprachzustand (für `useT`). */
|
||||
export function subscribeLanguage(cb: () => void): () => void {
|
||||
listeners.add(cb);
|
||||
return () => listeners.delete(cb);
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import { initCore } from "./core";
|
||||
import { getLanguage, setLanguage } from "./i18n";
|
||||
import type { Language } from "./i18n";
|
||||
import { applyThemeMode, getStoredThemeMode } from "./theme/themeMode";
|
||||
import "material-symbols/outlined.css";
|
||||
import "./styles.css";
|
||||
import "./text/fonts.css";
|
||||
|
||||
|
||||
Reference in new issue
Block a user