diff --git a/config/i18n.php b/config/i18n.php index 0befd15..fe730b0 100644 --- a/config/i18n.php +++ b/config/i18n.php @@ -278,23 +278,42 @@ function app_i18n_load_lang_json(string $lang): array /** * Nav-Anker für eine Seite aus der Sprachdatei holen. * - * Erwartete Struktur im JSON: + * Unterstützte Varianten im JSON: + * + * Variante A (direkte Labels): * "pages": { * "landing": { - * "meta": { ... }, * "anchors": { * "how": "So funktioniert USBCheck", - * "problem": "Warum gefälschte USB-Sticks gefährlich sind", - * ... + * "problem": "Warum gefälschte USB-Sticks gefährlich sind" + * } + * } + * } + * + * 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" } * } * } * } * * Rückgabe-Format: * [ - * [ 'href' => '#how', 'label' => 'So funktioniert USBCheck' ], - * [ 'href' => '#problem', 'label' => 'Warum gefälschte USB-Sticks gefährlich sind' ], - * ... + * [ 'href' => '#how', 'label' => 'So funktioniert USBCheck', 'i18n' => '' ], + * [ 'href' => '#problem', 'label' => '', 'i18n' => 'nav_problem' ], * ] */ function app_get_nav_anchors(string $pageKey): array @@ -302,23 +321,54 @@ function app_get_nav_anchors(string $pageKey): array $lang = $GLOBALS['lang'] ?? 'en'; $data = app_i18n_load_lang_json($lang); - $nav = $data['pages'][$pageKey]['anchors'] ?? null; - if (!is_array($nav)) { + $cfg = $data['pages'][$pageKey]['anchors'] ?? null; + if (!is_array($cfg)) { return []; } $anchors = []; - foreach ($nav as $id => $label) { - $id = trim((string)$id); - $label = trim((string)$label); - if ($id === '' || $label === '') { + foreach ($cfg as $id => $value) { + $id = trim((string)$id); + if ($id === '') { + continue; + } + + $href = '#' . $id; + $label = null; + $i18n = null; + + 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; + } + } + } elseif (is_array($value)) { + if (!empty($value['label'])) { + $label = (string)$value['label']; + } + if (!empty($value['i18n'])) { + $i18n = (string)$value['i18n']; + } + } + + // Wenn weder Label noch i18n da ist → überspringen + if ($label === null && $i18n === null) { continue; } $anchors[] = [ - 'href' => '#' . $id, - 'label' => $label, + 'href' => $href, + 'label' => $label ?? '', + 'i18n' => $i18n ?? '', ]; } diff --git a/partials/structure/header.php b/partials/structure/header.php index d093bde..695e65c 100644 --- a/partials/structure/header.php +++ b/partials/structure/header.php @@ -119,16 +119,27 @@ function build_lang_url(string $code, string $path, array $query): string
- - - + + + + +