修改缓存方式
This commit is contained in:
@@ -23,33 +23,87 @@ class LoadLangPack implements MiddlewareInterface
|
||||
return $handler($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析当前请求语言。
|
||||
* - 后台 admin:优先请求头 think-lang(zh-cn / en),其次 lang 头,再次查询/表单参数 lang(支持 zh→zh-cn)。
|
||||
* - 对外 api:优先查询/表单参数 lang(zh / en),其次 lang 头,再次 think-lang。
|
||||
*/
|
||||
protected function resolveLangSet(Request $request): string
|
||||
{
|
||||
$path = trim($request->path(), '/');
|
||||
$parts = explode('/', $path);
|
||||
$app = $parts[0] ?? 'api';
|
||||
|
||||
$queryRaw = $request->get('lang');
|
||||
if ($queryRaw === null || $queryRaw === '') {
|
||||
$queryRaw = $request->post('lang');
|
||||
}
|
||||
$queryLang = is_string($queryRaw) ? $queryRaw : '';
|
||||
|
||||
$thinkRaw = $request->header('think-lang');
|
||||
$thinkLang = is_string($thinkRaw) ? $thinkRaw : '';
|
||||
|
||||
$headerLangRaw = $request->header('lang');
|
||||
$headerLang = is_string($headerLangRaw) ? $headerLangRaw : '';
|
||||
|
||||
$normalize = static function (string $raw): string {
|
||||
$s = str_replace('_', '-', strtolower(trim($raw)));
|
||||
if ($s === 'zh') {
|
||||
return 'zh-cn';
|
||||
}
|
||||
|
||||
return $s;
|
||||
};
|
||||
|
||||
$allowLangList = config('lang.allow_lang_list', ['zh-cn', 'en']);
|
||||
|
||||
$candidates = [];
|
||||
if ($app === 'admin') {
|
||||
if ($thinkLang !== '') {
|
||||
$candidates[] = $normalize($thinkLang);
|
||||
}
|
||||
if ($headerLang !== '') {
|
||||
$candidates[] = $normalize($headerLang);
|
||||
}
|
||||
if ($queryLang !== '') {
|
||||
$candidates[] = $normalize($queryLang);
|
||||
}
|
||||
} else {
|
||||
if ($queryLang !== '') {
|
||||
$candidates[] = $normalize($queryLang);
|
||||
}
|
||||
if ($headerLang !== '') {
|
||||
$candidates[] = $normalize($headerLang);
|
||||
}
|
||||
if ($thinkLang !== '') {
|
||||
$candidates[] = $normalize($thinkLang);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($candidates as $code) {
|
||||
if ($code !== '' && in_array($code, $allowLangList, true)) {
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
|
||||
$acceptRaw = $request->header('accept-language');
|
||||
$acceptLang = is_string($acceptRaw) ? $acceptRaw : '';
|
||||
if (preg_match('/^zh[-_]?cn|^zh/i', $acceptLang)) {
|
||||
return 'zh-cn';
|
||||
}
|
||||
if (preg_match('/^en/i', $acceptLang)) {
|
||||
return 'en';
|
||||
}
|
||||
|
||||
$defaultRaw = config('lang.default_lang', config('translation.locale', 'zh-cn'));
|
||||
$default = is_string($defaultRaw) ? $defaultRaw : 'zh-cn';
|
||||
|
||||
return str_replace('_', '-', strtolower($default));
|
||||
}
|
||||
|
||||
protected function loadLang(Request $request): void
|
||||
{
|
||||
// 优先从请求头 lang / think-lang 获取前端选择的语言
|
||||
// 支持:lang=en / lang=zh / think-lang=en / think-lang=zh-cn
|
||||
// 未发送时回退到 Accept-Language 或配置默认值
|
||||
$headerLang = $request->header('lang', '');
|
||||
if ($headerLang === '') {
|
||||
$headerLang = $request->header('think-lang', '');
|
||||
}
|
||||
$allowLangList = config('lang.allow_lang_list', ['zh-cn', 'en']);
|
||||
$normalizedHeaderLang = str_replace('_', '-', strtolower($headerLang));
|
||||
if ($normalizedHeaderLang === 'zh') {
|
||||
$normalizedHeaderLang = 'zh-cn';
|
||||
}
|
||||
if ($headerLang && in_array($normalizedHeaderLang, $allowLangList)) {
|
||||
$langSet = $normalizedHeaderLang;
|
||||
} else {
|
||||
$acceptLang = $request->header('accept-language', '');
|
||||
if (preg_match('/^zh[-_]?cn|^zh/i', $acceptLang)) {
|
||||
$langSet = 'zh-cn';
|
||||
} elseif (preg_match('/^en/i', $acceptLang)) {
|
||||
$langSet = 'en';
|
||||
} else {
|
||||
$langSet = config('lang.default_lang', config('translation.locale', 'zh-cn'));
|
||||
}
|
||||
$langSet = str_replace('_', '-', strtolower($langSet));
|
||||
}
|
||||
$langSet = $this->resolveLangSet($request);
|
||||
|
||||
// 设置当前请求的翻译语言,使 __() 和 trans() 使用正确的语言
|
||||
if (function_exists('locale')) {
|
||||
@@ -74,6 +128,12 @@ class LoadLangPack implements MiddlewareInterface
|
||||
$translator->addResource('phpfile', $rootLangFile, $langSet, 'messages');
|
||||
}
|
||||
|
||||
// 1.5 公共语言包(app/common/lang/{locale}/*.php),供 common 服务层与多应用共用
|
||||
$commonLangPattern = base_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $langSet . DIRECTORY_SEPARATOR . '*.php';
|
||||
foreach (glob($commonLangPattern) ?: [] as $commonLangFile) {
|
||||
$translator->addResource('phpfile', $commonLangFile, $langSet, 'messages');
|
||||
}
|
||||
|
||||
// 2. 加载控制器专用语言包(如 zh-cn/auth/group.php),供 get_route_remark 等使用
|
||||
// 同时加载到 messages 域,使 __() 能正确翻译控制器内的文案(如安装页错误提示)
|
||||
$controllerPath = get_controller_path($request);
|
||||
|
||||
Reference in New Issue
Block a user