From 08e62ca338ec074f36ba460eb05c7fb9e754fa96 Mon Sep 17 00:00:00 2001 From: Lars Gebhardt-Kusche Date: Sat, 29 Nov 2025 01:08:56 +0100 Subject: [PATCH] upd --- config/i18n.php | 107 ++++++++++++++++++++++--------------- public/assets/i18n/de.json | 2 +- 2 files changed, 65 insertions(+), 44 deletions(-) diff --git a/config/i18n.php b/config/i18n.php index fe730b0..2621bc8 100644 --- a/config/i18n.php +++ b/config/i18n.php @@ -275,45 +275,59 @@ function app_i18n_load_lang_json(string $lang): array return $cache[$lang]; } +/** + * Aus einem Label einen stabilen i18n-Key für Nav-Anker bauen. + * Beispiel: "So funktioniert USBCheck!" -> "nav_so_funktioniert_usbcheck" + */ +function app_i18n_make_anchor_key(string $label): string +{ + // HTML-Entities entfernen (z. B. &) + $decoded = html_entity_decode($label, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + + // Kleinbuchstaben + $decoded = mb_strtolower($decoded, 'UTF-8'); + + // Alles, was kein a-z oder 0-9 ist, durch Unterstrich ersetzen + $key = preg_replace('/[^a-z0-9]+/u', '_', $decoded); + + // Mehrfache Unterstriche trimmen + $key = trim($key, '_'); + + if ($key === '') { + $key = 'item'; + } + + // Prefix, damit klar ist, dass es Navigationskeys sind + return 'nav_' . $key; +} + /** * Nav-Anker für eine Seite aus der Sprachdatei holen. * - * Unterstützte Varianten im JSON: + * Haupt-Variante im JSON: * - * Variante A (direkte Labels): * "pages": { * "landing": { * "anchors": { * "how": "So funktioniert USBCheck", - * "problem": "Warum gefälschte USB-Sticks gefährlich sind" + * "problem": "Warum gefälschte USB-Sticks gefährlich sind", + * "features": "Funktionen", + * "security": "Sicherheit", + * "faq": "FAQ" * } * } * } * - * Variante B (i18n-Keys): - * "pages": { - * "landing": { - * "anchors": { - * "how": "nav_how", - * "problem": "nav_problem" - * } - * } - * } - * - * Variante C (explizit): - * "pages": { - * "landing": { - * "anchors": { - * "how": { "label": "So funktioniert USBCheck", "i18n": "nav_how" }, - * "problem": { "i18n": "nav_problem" } - * } - * } + * Optional explizit: + * "anchors": { + * "how": { "label": "So funktioniert USBCheck", "i18n": "nav_how" }, + * "faq": { "i18n": "nav_faq" } * } * * Rückgabe-Format: * [ - * [ 'href' => '#how', 'label' => 'So funktioniert USBCheck', 'i18n' => '' ], - * [ 'href' => '#problem', 'label' => '', 'i18n' => 'nav_problem' ], + * [ 'href' => '#how', 'label' => 'So funktioniert USBCheck', 'i18n' => 'nav_so_funktioniert_usbcheck' ], + * [ 'href' => '#faq', 'label' => '', 'i18n' => 'nav_faq' ], * ] */ function app_get_nav_anchors(string $pageKey): array @@ -335,40 +349,47 @@ function app_get_nav_anchors(string $pageKey): array } $href = '#' . $id; - $label = null; - $i18n = null; + $label = ''; + $i18n = ''; if (is_string($value)) { - $valueTrim = trim($value); - - if ($valueTrim !== '') { - // Heuristik: - // - enthält der Wert Leerzeichen → fertiges Label - // - sonst → i18n-Key (z.B. "nav_how") - if (preg_match('/\s/u', $valueTrim)) { - $label = $valueTrim; - } else { - $i18n = $valueTrim; - } + // String IMMER als Label übernehmen + $labelTrim = trim($value); + if ($labelTrim === '') { + continue; } + + $label = $labelTrim; + // i18n-Key automatisch aus dem Label ableiten + $i18n = app_i18n_make_anchor_key($labelTrim); + } elseif (is_array($value)) { + // Explizite Variante: + // "how": { "label": "...", "i18n": "nav_how" } if (!empty($value['label'])) { - $label = (string)$value['label']; + $label = trim((string)$value['label']); } if (!empty($value['i18n'])) { - $i18n = (string)$value['i18n']; + $i18n = trim((string)$value['i18n']); } - } - // Wenn weder Label noch i18n da ist → überspringen - if ($label === null && $i18n === null) { + if ($label === '' && $i18n === '') { + continue; + } + + // Wenn Label gesetzt, aber kein i18n: automatisch generieren + if ($label !== '' && $i18n === '') { + $i18n = app_i18n_make_anchor_key($label); + } + } else { + // Weder String noch Array → ignorieren continue; } $anchors[] = [ 'href' => $href, - 'label' => $label ?? '', - 'i18n' => $i18n ?? '', + 'label' => $label, + 'i18n' => $i18n, ]; } diff --git a/public/assets/i18n/de.json b/public/assets/i18n/de.json index b9f623a..a08caaa 100755 --- a/public/assets/i18n/de.json +++ b/public/assets/i18n/de.json @@ -26,7 +26,7 @@ "anchors": { "how": "So funktioniert es", "problem": "Warum gefälschte Sticks gefährlich sind", - "features": "Funktionen und so", + "features": "Funktionen", "security": "Sicherheit", "faq": "FAQ" },