Add support for INI file sections in translations
Flatten INI sections into dot notation keys for easier access Improve language file filtering with pattern matching
This commit is contained in:
parent
ccfca94b57
commit
d54cdc7ce1
1 changed files with 32 additions and 6 deletions
|
|
@ -77,15 +77,38 @@ function loadTranslations(string $lang): array {
|
||||||
$defaultFile = dirname(__DIR__, 2) . "/default/languages/$lang.ini";
|
$defaultFile = dirname(__DIR__, 2) . "/default/languages/$lang.ini";
|
||||||
$customFile = dirname(__DIR__, 3) . "/custom/languages/$lang.ini";
|
$customFile = dirname(__DIR__, 3) . "/custom/languages/$lang.ini";
|
||||||
|
|
||||||
$translations = file_exists($defaultFile) ? parse_ini_file($defaultFile) ?: [] : [];
|
$translations = file_exists($defaultFile) ? flattenIniSections(parse_ini_file($defaultFile, true) ?: []) : [];
|
||||||
|
|
||||||
if (file_exists($customFile)) {
|
if (file_exists($customFile)) {
|
||||||
$translations = array_merge($translations, parse_ini_file($customFile) ?: []);
|
$translations = array_merge($translations, flattenIniSections(parse_ini_file($customFile, true) ?: []));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $translations;
|
return $translations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flatten INI sections into dot notation keys
|
||||||
|
* [section] key = value becomes section.key = value
|
||||||
|
* Top-level keys without sections are preserved as-is
|
||||||
|
*/
|
||||||
|
function flattenIniSections(array $ini): array {
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
foreach ($ini as $key => $value) {
|
||||||
|
if (is_array($value)) {
|
||||||
|
// This is a section - prefix all keys with section name
|
||||||
|
foreach ($value as $subKey => $subValue) {
|
||||||
|
$result["$key.$subKey"] = $subValue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Top-level key without section
|
||||||
|
$result[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
function formatDate(string $dateString, string $lang): string {
|
function formatDate(string $dateString, string $lang): string {
|
||||||
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $dateString, $m)) {
|
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $dateString, $m)) {
|
||||||
return $dateString;
|
return $dateString;
|
||||||
|
|
@ -116,10 +139,13 @@ function filterFilesByLanguage(array $files, string $dir, Context $ctx): array {
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
$parts = explode('.', $file['name']);
|
$parts = explode('.', $file['name']);
|
||||||
|
|
||||||
// Language-specific file (name.lang.ext)
|
// Language-specific file (name.lang.ext) - detect by 2-letter code pattern
|
||||||
if (count($parts) >= 3 && in_array($parts[count($parts) - 2], $availableLangs)) {
|
$potentialLang = $parts[count($parts) - 2] ?? '';
|
||||||
$fileLang = $parts[count($parts) - 2];
|
$isLangFile = count($parts) >= 3 && preg_match('/^[a-z]{2}$/', $potentialLang);
|
||||||
if ($fileLang === $currentLang) {
|
|
||||||
|
if ($isLangFile) {
|
||||||
|
// Only include if it matches current language AND is available
|
||||||
|
if ($potentialLang === $currentLang && in_array($potentialLang, $availableLangs)) {
|
||||||
$filtered[] = $file;
|
$filtered[] = $file;
|
||||||
$seen[$parts[0]] = true;
|
$seen[$parts[0]] = true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue