107 lines
3.7 KiB
PHP
107 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace {
|
|
|
|
class Yii
|
|
{
|
|
/** @var object */
|
|
public static $app;
|
|
|
|
/** @var array<string, array<string, array<string, string>>> */
|
|
private static array $messages = [];
|
|
|
|
/** @var string */
|
|
public static string $language = 'ru';
|
|
|
|
/** @var array<string> */
|
|
public static array $supportedLanguages = ['ru', 'en'];
|
|
|
|
/**
|
|
* Initialize i18n and mock Yii::$app
|
|
*
|
|
* @param string $defaultLang
|
|
*/
|
|
public static function init(string $defaultLang = 'ru'): void
|
|
{
|
|
$lang = null;
|
|
|
|
// // 1. Language from query parameter (e.g. ?lang=en)
|
|
// if (!empty($_GET['lang']) && is_string($_GET['lang'])) {
|
|
// $candidate = strtolower(trim($_GET['lang']));
|
|
// if (in_array($candidate, self::$supportedLanguages, true)) {
|
|
// $lang = $candidate;
|
|
// }
|
|
// }
|
|
|
|
// // 2. Language from cookie
|
|
// if ($lang === null && !empty($_COOKIE['lang']) && is_string($_COOKIE['lang'])) {
|
|
// $candidate = strtolower(trim($_COOKIE['lang']));
|
|
// if (in_array($candidate, self::$supportedLanguages, true)) {
|
|
// $lang = $candidate;
|
|
// }
|
|
// }
|
|
|
|
// // 3. Language from Accept-Language header
|
|
// if ($lang === null && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) && is_string($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
|
// $candidate = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
|
|
// if (in_array($candidate, self::$supportedLanguages, true)) {
|
|
// $lang = $candidate;
|
|
// }
|
|
// }
|
|
|
|
$lang = 'ru';
|
|
|
|
self::$language = $lang ?? $defaultLang;
|
|
}
|
|
|
|
/**
|
|
* Translates a message to the specified language.
|
|
*
|
|
* @param string $category Message category (e.g. 'app', 'error')
|
|
* @param string $message The message to be translated
|
|
* @param array $params Parameters to replace in the message
|
|
* @param string|null $language Target language, or null for current
|
|
* @return string
|
|
*/
|
|
public static function t(string $category, string $message, array $params = [], ?string $language = null): string
|
|
{
|
|
$lang = $language ?: self::$language;
|
|
|
|
if (!isset(self::$messages[$lang][$category])) {
|
|
$file = __DIR__ . '/messages/' . $lang . '/' . $category . '.php';
|
|
if (file_exists($file)) {
|
|
self::$messages[$lang][$category] = (array)(require $file);
|
|
} else {
|
|
self::$messages[$lang][$category] = [];
|
|
}
|
|
}
|
|
|
|
$translation = self::$messages[$lang][$category][$message] ?? $message;
|
|
|
|
// Handle Yii2 deprecated/empty marker
|
|
if ($translation === '' || (is_string($translation) && strncmp($translation, '@@', 2) === 0)) {
|
|
$translation = $message;
|
|
}
|
|
|
|
if (!empty($params)) {
|
|
$replace = [];
|
|
foreach ($params as $key => $value) {
|
|
$replace['{' . $key . '}'] = (string)$value;
|
|
}
|
|
$translation = strtr($translation, $replace);
|
|
}
|
|
|
|
return $translation;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Standalone shorthand helper for translations
|
|
*/
|
|
if (!function_exists('t')) {
|
|
function t(string $category, string $message, array $params = [], ?string $language = null): string
|
|
{
|
|
return Yii::t($category, $message, $params, $language);
|
|
}
|
|
}
|
|
} |