From 8817effc9a02d268ba05beea6ba0f63ef305c4c0 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 25 Feb 2026 18:18:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E5=AE=98?= =?UTF-8?q?=E6=96=B9support\Translation=E6=9B=BF=E4=BB=A3=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89Lang=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 config/translation.php 配置文件 - 将翻译文件从 lang/ 移动到 resource/translations/ 目录 - 按照 Symfony Translation 标准重命名文件 (messages.{locale}.php) - 删除自定义的 app\util\Lang 类 - 使用官方的 trans() 和 locale() 函数 Co-Authored-By: Claude Opus 4.6 --- backend/app/util/Lang.php | 170 ------------------ backend/config/translation.php | 12 ++ .../translations/messages.en.php} | 0 .../translations/messages.es.php} | 0 .../translations/messages.fr.php} | 0 .../translations/messages.ru.php} | 0 .../translations/messages.zh.php} | 0 7 files changed, 12 insertions(+), 170 deletions(-) delete mode 100644 backend/app/util/Lang.php create mode 100644 backend/config/translation.php rename backend/{lang/en.php => resource/translations/messages.en.php} (100%) rename backend/{lang/es.php => resource/translations/messages.es.php} (100%) rename backend/{lang/fr.php => resource/translations/messages.fr.php} (100%) rename backend/{lang/ru.php => resource/translations/messages.ru.php} (100%) rename backend/{lang/zh.php => resource/translations/messages.zh.php} (100%) diff --git a/backend/app/util/Lang.php b/backend/app/util/Lang.php deleted file mode 100644 index 5fc621c..0000000 --- a/backend/app/util/Lang.php +++ /dev/null @@ -1,170 +0,0 @@ - '简体中文', - 'en' => 'English', - 'ru' => 'Русский', - 'es' => 'Español', - 'fr' => 'Français', - ]; - - /** - * 初始化翻译 - */ - private static function loadTranslations(): void - { - if (self::$translations !== null) { - return; - } - - self::$translations = []; - foreach (self::SUPPORTED_LOCALES as $locale) { - $file = base_path() . "/lang/{$locale}.php"; - if (file_exists($file)) { - self::$translations[$locale] = require $file; - } - } - } - - /** - * 设置当前语言 - */ - public static function setLocale(string $locale): void - { - if (in_array($locale, self::SUPPORTED_LOCALES)) { - self::$locale = $locale; - } - } - - /** - * 获取当前语言 - */ - public static function getLocale(): string - { - return self::$locale; - } - - /** - * 从请求中获取语言 - */ - public static function detectFromRequest(): string - { - // 从请求头获取语言 - $request = \request(); - $acceptLanguage = $request->header('Accept-Language'); - - if ($acceptLanguage) { - // 解析 Accept-Language 头 - $locales = explode(',', $acceptLanguage); - foreach ($locales as $locale) { - $locale = trim(strstr($locale, ';', true)); - $locale = substr($locale, 0, 2); - if (in_array($locale, self::SUPPORTED_LOCALES)) { - return $locale; - } - } - } - - // 从查询参数获取 - $lang = $request->input('lang'); - if ($lang && in_array($lang, self::SUPPORTED_LOCALES)) { - return $lang; - } - - return self::$fallbackLocale; - } - - /** - * 翻译文本 - * - * @param string $key 翻译键,使用点号分隔,如 'common.confirm' - * @param array $params 替换参数 - * @param string|null $locale 指定语言,null 则使用当前语言 - * @return string 翻译后的文本 - */ - public static function get(string $key, array $params = [], ?string $locale = null): string - { - self::loadTranslations(); - - $locale = $locale ?? self::$locale; - - // 获取翻译值 - $value = self::getTranslationValue($key, $locale); - - // 替换参数 - if (!empty($params)) { - foreach ($params as $search => $replace) { - $value = str_replace(':' . $search, $replace, $value); - $value = str_replace('{' . $search . '}', $replace, $value); - } - } - - return $value; - } - - /** - * 获取翻译值 - */ - private static function getTranslationValue(string $key, string $locale): string - { - $keys = explode('.', $key); - $value = self::$translations[$locale] ?? []; - - foreach ($keys as $k) { - if (isset($value[$k])) { - $value = $value[$k]; - } else { - // 尝试使用回退语言 - if ($locale !== self::$fallbackLocale) { - return self::getTranslationValue($key, self::$fallbackLocale); - } - return $key; - } - } - - return is_string($value) ? $value : $key; - } - - /** - * 判断翻译是否存在 - */ - public static function has(string $key, ?string $locale = null): bool - { - self::loadTranslations(); - - $locale = $locale ?? self::$locale; - - $keys = explode('.', $key); - $value = self::$translations[$locale] ?? []; - - foreach ($keys as $k) { - if (isset($value[$k])) { - $value = $value[$k]; - } else { - return false; - } - } - - return true; - } -} diff --git a/backend/config/translation.php b/backend/config/translation.php new file mode 100644 index 0000000..a7d9e68 --- /dev/null +++ b/backend/config/translation.php @@ -0,0 +1,12 @@ + 'zh', + // Fallback language + 'fallback_locale' => ['zh', 'en'], + // Folder where language files are stored + 'path' => base_path() . '/resource/translations', +]; diff --git a/backend/lang/en.php b/backend/resource/translations/messages.en.php similarity index 100% rename from backend/lang/en.php rename to backend/resource/translations/messages.en.php diff --git a/backend/lang/es.php b/backend/resource/translations/messages.es.php similarity index 100% rename from backend/lang/es.php rename to backend/resource/translations/messages.es.php diff --git a/backend/lang/fr.php b/backend/resource/translations/messages.fr.php similarity index 100% rename from backend/lang/fr.php rename to backend/resource/translations/messages.fr.php diff --git a/backend/lang/ru.php b/backend/resource/translations/messages.ru.php similarity index 100% rename from backend/lang/ru.php rename to backend/resource/translations/messages.ru.php diff --git a/backend/lang/zh.php b/backend/resource/translations/messages.zh.php similarity index 100% rename from backend/lang/zh.php rename to backend/resource/translations/messages.zh.php