This commit is contained in:
2025-11-28 03:31:46 +01:00
parent 5de109b649
commit cf68494f55
7 changed files with 132 additions and 12 deletions

View File

@@ -232,3 +232,95 @@ function app_i18n_get_frontend_config(): array
'available' => $GLOBALS['availableLangs'] ?? [],
];
}
/**
* Komplette JSON-Struktur für eine Sprache laden.
* Nutzt einfachen Request-Cache, damit pro Sprache nur einmal von Platte gelesen wird.
*/
function app_i18n_load_lang_json(string $lang): array
{
static $cache = [];
$lang = strtolower(substr($lang, 0, 5));
if (isset($cache[$lang])) {
return $cache[$lang];
}
$baseDir = realpath(__DIR__ . '/../public/assets/i18n');
if ($baseDir === false) {
$cache[$lang] = [];
return $cache[$lang];
}
$path = $baseDir . '/' . $lang . '.json';
if (!is_file($path)) {
// Fallback: en.json, falls vorhanden
$fallback = $baseDir . '/en.json';
if (is_file($fallback)) {
$json = @file_get_contents($fallback);
$data = json_decode($json, true);
$cache[$lang] = is_array($data) ? $data : [];
return $cache[$lang];
}
$cache[$lang] = [];
return $cache[$lang];
}
$json = @file_get_contents($path);
$data = json_decode($json, true);
$cache[$lang] = is_array($data) ? $data : [];
return $cache[$lang];
}
/**
* Nav-Anker für eine Seite aus der Sprachdatei holen.
*
* Erwartete Struktur im JSON:
* "pages": {
* "landing": {
* "meta": { ... },
* "anchors": {
* "how": "So funktioniert USBCheck",
* "problem": "Warum gefälschte USB-Sticks gefährlich sind",
* ...
* }
* }
* }
*
* Rückgabe-Format:
* [
* [ 'href' => '#how', 'label' => 'So funktioniert USBCheck' ],
* [ 'href' => '#problem', 'label' => 'Warum gefälschte USB-Sticks gefährlich sind' ],
* ...
* ]
*/
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)) {
return [];
}
$anchors = [];
foreach ($nav as $id => $label) {
$id = trim((string)$id);
$label = trim((string)$label);
if ($id === '' || $label === '') {
continue;
}
$anchors[] = [
'href' => '#' . $id,
'label' => $label,
];
}
return $anchors;
}