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:
Ruben 2026-01-16 22:03:15 +01:00
parent ccfca94b57
commit d54cdc7ce1

View file

@ -77,15 +77,38 @@ function loadTranslations(string $lang): array {
$defaultFile = dirname(__DIR__, 2) . "/default/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)) {
$translations = array_merge($translations, parse_ini_file($customFile) ?: []);
$translations = array_merge($translations, flattenIniSections(parse_ini_file($customFile, true) ?: []));
}
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 {
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $dateString, $m)) {
return $dateString;
@ -116,10 +139,13 @@ function filterFilesByLanguage(array $files, string $dir, Context $ctx): array {
foreach ($files as $file) {
$parts = explode('.', $file['name']);
// Language-specific file (name.lang.ext)
if (count($parts) >= 3 && in_array($parts[count($parts) - 2], $availableLangs)) {
$fileLang = $parts[count($parts) - 2];
if ($fileLang === $currentLang) {
// Language-specific file (name.lang.ext) - detect by 2-letter code pattern
$potentialLang = $parts[count($parts) - 2] ?? '';
$isLangFile = count($parts) >= 3 && preg_match('/^[a-z]{2}$/', $potentialLang);
if ($isLangFile) {
// Only include if it matches current language AND is available
if ($potentialLang === $currentLang && in_array($potentialLang, $availableLangs)) {
$filtered[] = $file;
$seen[$parts[0]] = true;
}