folderweb/app/plugins/global/languages.php

86 lines
2.7 KiB
PHP
Raw Normal View History

<?php
// Languages plugin - translation loading and filtering
function loadTranslations(string $lang): array {
$defaultTranslationFile = dirname(__DIR__, 2) . "/default/languages/$lang.ini";
$customTranslationFile = dirname(__DIR__, 3) . "/custom/languages/$lang.ini";
$translations = [];
if (file_exists($defaultTranslationFile)) {
$translations = parse_ini_file($defaultTranslationFile) ?: [];
}
if (file_exists($customTranslationFile)) {
$customTranslations = parse_ini_file($customTranslationFile) ?: [];
$translations = array_merge($translations, $customTranslations);
}
return $translations;
}
function shouldHideUntranslated(): bool {
$configFile = file_exists(__DIR__ . '/../../../custom/config.ini')
? __DIR__ . '/../../../custom/config.ini'
: __DIR__ . '/../../config.ini';
if (!file_exists($configFile)) return true;
$config = parse_ini_file($configFile, true);
return !isset($config['languages']['show_untranslated'])
|| $config['languages']['show_untranslated'] !== 'true';
}
function hasLanguageMetadata(string $dirPath, string $lang): bool {
$metadataFile = "$dirPath/metadata.ini";
if (!file_exists($metadataFile)) return false;
$metadata = parse_ini_file($metadataFile, true);
if (!$metadata) return false;
if (!isset($metadata[$lang]) || !is_array($metadata[$lang])) return false;
// Check if language section has meaningful content (title or summary)
return isset($metadata[$lang]['title']) || isset($metadata[$lang]['summary']);
}
function hasLanguageContent(string $dirPath, string $lang, array $contentExtensions): bool {
if (!is_dir($dirPath)) return false;
$files = scandir($dirPath) ?: [];
foreach ($files as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (!in_array($ext, $contentExtensions)) continue;
$parts = explode('.', $file);
if (count($parts) >= 3) {
$fileLang = $parts[count($parts) - 2];
if ($fileLang === $lang && is_file("$dirPath/$file")) {
return true;
}
}
}
return false;
}
function formatDate(string $dateString, string $lang): string {
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $dateString, $matches)) {
return $dateString;
}
$translations = loadTranslations($lang);
$day = (int)$matches[3];
$monthIndex = (int)$matches[2] - 1;
$year = $matches[1];
if (isset($translations['months'])) {
$months = array_map('trim', explode(',', $translations['months']));
$month = $months[$monthIndex] ?? $matches[2];
} else {
$month = $matches[2];
}
return "$day. $month $year";
}