This commit is contained in:
2025-11-27 23:41:44 +01:00
parent ae51cec1b5
commit 8e895f5226
4 changed files with 380 additions and 354 deletions

View File

@@ -110,7 +110,11 @@ function simpleKeyExistsRecursive(array $data, string $key): bool
return false;
}
function addSimpleKey(array &$data, string $key, string $defaultValue = null): void
/**
* Fügt einen einfachen Key unter $data['auto'] hinzu,
* sofern er nicht irgendwo in der Struktur bereits existiert.
*/
function addSimpleKey(array &$data, string $key, ?string $defaultValue = null): void
{
if (simpleKeyExistsRecursive($data, $key)) {
return;
@@ -120,12 +124,13 @@ function addSimpleKey(array &$data, string $key, string $defaultValue = null): v
$data['auto'] = [];
}
if ($defaultValue === null || $defaultValue === '') {
$defaultValue = $key;
}
// Default: übergebener Wert oder der Key selbst
$value = ($defaultValue !== null && $defaultValue !== '')
? $defaultValue
: $key;
if (!array_key_exists($key, $data['auto'])) {
$data['auto'][$key] = $defaultValue;
$data['auto'][$key] = $value;
}
}
@@ -154,9 +159,8 @@ function addKeyToData(array &$data, string $key, ?string $defaultValue = null):
/**
* Extrahiert den „aktuellen Inhalt“ eines data-i18n-Elements
* ganz grob über Regex:
* grob über Regex:
* ... data-i18n="key"> Inhalt </...
* Ist nur ein Best-Effort; bei komplexeren Strukturen fällt es auf null zurück.
*/
function extractInlineTextForDataI18n(string $content, int $matchOffset, int $matchLength): ?string
{
@@ -201,14 +205,23 @@ function collectKeysFromFile(string $file): array
$keysWithDefaults = [];
// --- data-i18n="key" / 'key' ---
if (preg_match_all('/data-i18n\s*=\s*(["\'])(.+?)\1/i', $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
if (preg_match_all(
'/data-i18n\s*=\s*(["\'])(.+?)\1/i',
$content,
$matches,
PREG_SET_ORDER | PREG_OFFSET_CAPTURE
)) {
foreach ($matches as $m) {
$fullMatch = $m[0][0];
$fullOffset = $m[0][1];
$key = trim($m[2][0]);
// Versuche, den Inline-Text zu extrahieren: >Text<
$inlineText = extractInlineTextForDataI18n($content, $fullOffset, strlen($fullMatch));
$inlineText = extractInlineTextForDataI18n(
$content,
$fullOffset,
strlen($fullMatch)
);
if (!array_key_exists($key, $keysWithDefaults)) {
$keysWithDefaults[$key] = $inlineText;
@@ -290,7 +303,7 @@ foreach ($scanDirs as $dir) {
continue;
}
$filePath = $fileInfo->getPathname();
$filePath = $fileInfo->getPathname();
$keysInFile = collectKeysFromFile($filePath);
foreach ($keysInFile as $key => $defaultText) {
@@ -366,6 +379,9 @@ if (!empty($newKeys)) {
if ($isCli) {
echo $output;
} else {
header('Content-Type: text/plain; charset=utf-8');
// Nur Header setzen, wenn noch keine gesendet wurden
if (!headers_sent()) {
header('Content-Type: text/plain; charset=utf-8');
}
echo $output;
}