/
delphin
/
CatRW
Обзор
Документация
Войти
/
delphin
/
CatRW
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
api/TranslationService.php
81 строка
3 KB
delphin
start project
02 мар 2026, 09:14
Верифицирован
02 мар 2026, 09:14
4b0ee03
Код
Авторство
О чём код?
<?php // /api/TranslationService.php (доработанный) class TranslationService { private static $dict = []; private static $lang = 'en'; public static function init($lang = null) { // Если язык передан, проверяем и устанавливаем if ($lang && preg_match('/^[a-z]{2}$/', $lang)) { self::$lang = $lang; } // Загружаем файл перевода $file = ROOT_PATH . "/locales/" . self::$lang . ".json"; if (file_exists($file)) { $content = file_get_contents($file); self::$dict = json_decode($content, true) ?? []; } else { // Fallback на английский $file_en = ROOT_PATH . "/locales/en.json"; if (file_exists($file_en)) { $content = file_get_contents($file_en); self::$dict = json_decode($content, true) ?? []; } } // Устанавливаем куки на год setcookie('lang', self::$lang, time() + 31536000, '/'); } public static function t($cat, $code) { return self::$dict[$cat][$code] ?? $code; } public static function ui($key) { return self::$dict['ui'][$key] ?? $key; } public static function get($key, $params = []) { // Поддержка dot notation: 'types.locomotive' $parts = explode('.', $key, 2); if (count($parts) === 2) { list($cat, $code) = $parts; $translation = self::$dict[$cat][$code] ?? $code; } else { $translation = self::$dict['ui'][$key] ?? $key; } // Простая замена параметров {param} foreach ($params as $param => $value) { $translation = str_replace('{' . $param . '}', $value, $translation); } return $translation; } public static function getLang() { return self::$lang; } public static function getAvailableLanguages() { $languages = []; $locales_path = ROOT_PATH . "/locales/"; if (is_dir($locales_path)) { $files = glob($locales_path . "*.json"); foreach ($files as $file) { $lang_code = pathinfo($file, PATHINFO_FILENAME); $languages[] = $lang_code; } } return $languages; } public static function getAll() { return self::$dict; } } ?>