i18
This commit is contained in:
@@ -113,6 +113,9 @@ function simpleKeyExistsRecursive(array $data, string $key): bool
|
|||||||
/**
|
/**
|
||||||
* Fügt einen einfachen Key unter $data['auto'] hinzu,
|
* Fügt einen einfachen Key unter $data['auto'] hinzu,
|
||||||
* sofern er nicht irgendwo in der Struktur bereits existiert.
|
* sofern er nicht irgendwo in der Struktur bereits existiert.
|
||||||
|
*
|
||||||
|
* WICHTIG: $defaultValue ist explizit nullable, um
|
||||||
|
* deprecation unter PHP 8.4+ zu vermeiden.
|
||||||
*/
|
*/
|
||||||
function addSimpleKey(array &$data, string $key, ?string $defaultValue = null): void
|
function addSimpleKey(array &$data, string $key, ?string $defaultValue = null): void
|
||||||
{
|
{
|
||||||
@@ -124,13 +127,12 @@ function addSimpleKey(array &$data, string $key, ?string $defaultValue = null):
|
|||||||
$data['auto'] = [];
|
$data['auto'] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default: übergebener Wert oder der Key selbst
|
if ($defaultValue === null || $defaultValue === '') {
|
||||||
$value = ($defaultValue !== null && $defaultValue !== '')
|
$defaultValue = $key;
|
||||||
? $defaultValue
|
}
|
||||||
: $key;
|
|
||||||
|
|
||||||
if (!array_key_exists($key, $data['auto'])) {
|
if (!array_key_exists($key, $data['auto'])) {
|
||||||
$data['auto'][$key] = $value;
|
$data['auto'][$key] = $defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,8 +161,9 @@ function addKeyToData(array &$data, string $key, ?string $defaultValue = null):
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Extrahiert den „aktuellen Inhalt“ eines data-i18n-Elements
|
* Extrahiert den „aktuellen Inhalt“ eines data-i18n-Elements
|
||||||
* grob über Regex:
|
* ganz grob über Regex:
|
||||||
* ... data-i18n="key"> Inhalt </...
|
* ... 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
|
function extractInlineTextForDataI18n(string $content, int $matchOffset, int $matchLength): ?string
|
||||||
{
|
{
|
||||||
@@ -205,23 +208,14 @@ function collectKeysFromFile(string $file): array
|
|||||||
$keysWithDefaults = [];
|
$keysWithDefaults = [];
|
||||||
|
|
||||||
// --- data-i18n="key" / 'key' ---
|
// --- data-i18n="key" / 'key' ---
|
||||||
if (preg_match_all(
|
if (preg_match_all('/data-i18n\s*=\s*(["\'])(.+?)\1/i', $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
|
||||||
'/data-i18n\s*=\s*(["\'])(.+?)\1/i',
|
|
||||||
$content,
|
|
||||||
$matches,
|
|
||||||
PREG_SET_ORDER | PREG_OFFSET_CAPTURE
|
|
||||||
)) {
|
|
||||||
foreach ($matches as $m) {
|
foreach ($matches as $m) {
|
||||||
$fullMatch = $m[0][0];
|
$fullMatch = $m[0][0];
|
||||||
$fullOffset = $m[0][1];
|
$fullOffset = $m[0][1];
|
||||||
$key = trim($m[2][0]);
|
$key = trim($m[2][0]);
|
||||||
|
|
||||||
// Versuche, den Inline-Text zu extrahieren: >Text<
|
// Versuche, den Inline-Text zu extrahieren: >Text<
|
||||||
$inlineText = extractInlineTextForDataI18n(
|
$inlineText = extractInlineTextForDataI18n($content, $fullOffset, strlen($fullMatch));
|
||||||
$content,
|
|
||||||
$fullOffset,
|
|
||||||
strlen($fullMatch)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!array_key_exists($key, $keysWithDefaults)) {
|
if (!array_key_exists($key, $keysWithDefaults)) {
|
||||||
$keysWithDefaults[$key] = $inlineText;
|
$keysWithDefaults[$key] = $inlineText;
|
||||||
@@ -379,7 +373,7 @@ if (!empty($newKeys)) {
|
|||||||
if ($isCli) {
|
if ($isCli) {
|
||||||
echo $output;
|
echo $output;
|
||||||
} else {
|
} else {
|
||||||
// Nur Header setzen, wenn noch keine gesendet wurden
|
// Header nur setzen, wenn noch nichts gesendet wurde
|
||||||
if (!headers_sent()) {
|
if (!headers_sent()) {
|
||||||
header('Content-Type: text/plain; charset=utf-8');
|
header('Content-Type: text/plain; charset=utf-8');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user