From 570e5a97b42042b48572add7173ff995a4e9cac4 Mon Sep 17 00:00:00 2001 From: Lars Gebhardt-Kusche Date: Fri, 28 Nov 2025 00:42:40 +0100 Subject: [PATCH] ss --- public/assets/i18n/de.json | 2 +- tools/i18n_collect_keys.php | 61 +++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/public/assets/i18n/de.json b/public/assets/i18n/de.json index 8e9dd01..2a9f11e 100644 --- a/public/assets/i18n/de.json +++ b/public/assets/i18n/de.json @@ -5,4 +5,4 @@ "flag": "🇩🇪", "enabled": true } -} \ No newline at end of file +} diff --git a/tools/i18n_collect_keys.php b/tools/i18n_collect_keys.php index 8da1d9b..1dd391a 100644 --- a/tools/i18n_collect_keys.php +++ b/tools/i18n_collect_keys.php @@ -5,7 +5,7 @@ * Sammelt alle verwendeten Übersetzungs-Keys aus: * - /public/landingpage/{*}/{*} * - /partials/landing/{*}/{*} - * - /partials/structure/{*}/{*} + * - /partials/structure/{*}.php * - (optional) /partials/partials/{*}/{*}, falls vorhanden * * und trägt fehlende Keys in public/assets/i18n/de.json ein. @@ -111,11 +111,10 @@ function simpleKeyExistsRecursive(array $data, string $key): bool } /** - * Fügt einen einfachen Key unter $data['auto'] hinzu, - * sofern er nicht irgendwo in der Struktur bereits existiert. + * Fügt einen einfachen Key irgendwo in der Struktur ein + * (früher unter $data['auto'], jetzt nur noch für Reste). * - * WICHTIG: $defaultValue ist explizit nullable, um - * deprecation unter PHP 8.4+ zu vermeiden. + * WICHTIG: $defaultValue ist explizit nullable → keine Deprecation. */ function addSimpleKey(array &$data, string $key, ?string $defaultValue = null): void { @@ -139,7 +138,9 @@ function addSimpleKey(array &$data, string $key, ?string $defaultValue = null): /** * Key einfügen, mit optionalem Default-Text. * Dot-Notation → verschachtelt, - * Simple-Key → unter "auto". + * Simple-Key → (Rest) unter "auto". + * + * Dynamische Keys mit $ (z.B. pages.$pageKey.meta.title) werden ignoriert. */ function addKeyToData(array &$data, string $key, ?string $defaultValue = null): void { @@ -202,6 +203,9 @@ function extractInlineTextForDataI18n(string $content, int $matchOffset, int $ma * * Rückgabe: * [ 'key1' => 'Default-Text oder null', 'key2' => null, ... ] + * + * Dynamische Keys mit $ (pages.$pageKey...) werden bereits hier + * ignoriert. */ function collectKeysFromFile(string $file): array { @@ -219,6 +223,10 @@ function collectKeysFromFile(string $file): array $fullOffset = $m[0][1]; $key = trim($m[2][0]); + if (strpos($key, '$') !== false) { + continue; + } + // Versuche, den Inline-Text zu extrahieren: >Text< $inlineText = extractInlineTextForDataI18n($content, $fullOffset, strlen($fullMatch)); @@ -239,9 +247,8 @@ function collectKeysFromFile(string $file): array PREG_SET_ORDER )) { foreach ($m2 as $match) { - $key = trim($match[2]); + $key = trim($match[2]); - // Dynamische Keys mit Variablen wie pages.$pageKey.* hier sofort ignorieren if (strpos($key, '$') !== false) { continue; } @@ -267,9 +274,8 @@ function collectKeysFromFile(string $file): array PREG_SET_ORDER )) { foreach ($m3 as $match) { - $key = trim($match[2]); + $key = trim($match[2]); - // Auch hier dynamische Keys mit $ ignorieren if (strpos($key, '$') !== false) { continue; } @@ -314,10 +320,41 @@ foreach ($scanDirs as $dir) { continue; } - $filePath = $fileInfo->getPathname(); + $filePath = $fileInfo->getPathname(); + $relPath = str_replace($baseDir . DIRECTORY_SEPARATOR, '', $filePath); + $keysInFile = collectKeysFromFile($filePath); foreach ($keysInFile as $key => $defaultText) { + $origKey = $key; + + // Nur für einfache Keys (ohne Punkt) Kontext-basiert umschreiben + if (strpos($key, '.') === false) { + + // 4a) partials/landing/{slug}/{section}.php + if (preg_match('~^partials/landing/([^/]+)/([^/]+)\.(php|html?|phtml)$~', $relPath, $m)) { + $slug = $m[1]; // z.B. landing, fakecheck, login, dashboard + $section = $m[2]; // z.B. hero, how, problem, features, security, faq, main + + $key = "pages.$slug.sections.$section.$key"; + + // 4b) public/landingpage/{slug}/*.php → Fallback: section "main" + } elseif (preg_match('~^public/landingpage/([^/]+)/.+\.(php|html?|phtml)$~', $relPath, $m)) { + $slug = $m[1]; // z.B. landing, fakecheck, login, dashboard + + $key = "pages.$slug.sections.main.$key"; + + // 4c) partials/structure/{name}.php → z.B. header.*, footer.* + } elseif (preg_match('~^partials/structure/([^/]+)\.(php|html?|phtml)$~', $relPath, $m)) { + $section = $m[1]; // header, footer, layout_start, layout_end, app_config, ... + + // Für header/footer/… legen wir den Root so an, wie die Datei heißt: + // header_slogan → header.header_slogan + $key = $section . '.' . $key; + } + } + + // Gefilterten Key übernehmen if (!array_key_exists($key, $foundKeys)) { $foundKeys[$key] = $defaultText; } else { @@ -366,7 +403,7 @@ if (is_dir($landingRoot)) { $slug = $entry->getFilename(); - // sehr simple Heuristik: Landingpage gilt als "aktiv", wenn irgendeine .php drin liegt + // einfache Heuristik: Landingpage gilt als "aktiv", wenn irgendeine .php drin liegt $hasPhp = false; foreach (new DirectoryIterator($entry->getPathname()) as $file) { if ($file->isFile() && strtolower($file->getExtension()) === 'php') {