1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think\middleware; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
use think\App; |
17
|
|
|
use think\Config; |
18
|
|
|
use think\Cookie; |
19
|
|
|
use think\Lang; |
20
|
|
|
use think\Request; |
21
|
|
|
use think\Response; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* 多语言加载 |
25
|
|
|
*/ |
26
|
|
|
class LoadLangPack |
27
|
|
|
{ |
28
|
|
|
protected $app; |
29
|
|
|
protected $lang; |
30
|
|
|
protected $config; |
31
|
|
|
|
32
|
|
|
public function __construct(App $app, Lang $lang, Config $config) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->app = $app; |
35
|
|
|
$this->lang = $lang; |
36
|
|
|
$this->config = $lang->getConfig(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 路由初始化(路由规则注册) |
41
|
|
|
* @access public |
42
|
|
|
* @param Request $request |
43
|
|
|
* @param Closure $next |
44
|
|
|
* @return Response |
45
|
|
|
*/ |
46
|
|
|
public function handle($request, Closure $next) |
47
|
|
|
{ |
48
|
|
|
// 自动侦测当前语言 |
49
|
|
|
$langset = $this->detect($request); |
50
|
|
|
|
51
|
|
|
if ($this->lang->defaultLangSet() != $langset) { |
52
|
|
|
$this->lang->switchLangSet($langset); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->saveToCookie($this->app->cookie, $langset); |
56
|
|
|
|
57
|
|
|
return $next($request); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 自动侦测设置获取语言选择 |
62
|
|
|
* @access protected |
63
|
|
|
* @param Request $request |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function detect(Request $request): string |
67
|
|
|
{ |
68
|
|
|
// 自动侦测设置获取语言选择 |
69
|
|
|
$langSet = ''; |
70
|
|
|
|
71
|
|
|
if ($request->get($this->config['detect_var'])) { |
72
|
|
|
// url中设置了语言变量 |
73
|
|
|
$langSet = strtolower($request->get($this->config['detect_var'])); |
74
|
|
|
} elseif ($request->header($this->config['header_var'])) { |
75
|
|
|
// Header中设置了语言变量 |
76
|
|
|
$langSet = strtolower($request->header($this->config['header_var'])); |
|
|
|
|
77
|
|
|
} elseif ($request->cookie($this->config['cookie_var'])) { |
78
|
|
|
// Cookie中设置了语言变量 |
79
|
|
|
$langSet = strtolower($request->cookie($this->config['cookie_var'])); |
80
|
|
|
} elseif ($request->server('HTTP_ACCEPT_LANGUAGE')) { |
81
|
|
|
// 自动侦测浏览器语言 |
82
|
|
|
$match = preg_match('/^([a-z\d\-]+)/i', $request->server('HTTP_ACCEPT_LANGUAGE'), $matches); |
83
|
|
|
if ($match) { |
84
|
|
|
$langSet = strtolower($matches[1]); |
85
|
|
|
if (isset($this->config['accept_language'][$langSet])) { |
86
|
|
|
$langSet = $this->config['accept_language'][$langSet]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (empty($this->config['allow_lang_list']) || in_array($langSet, $this->config['allow_lang_list'])) { |
92
|
|
|
// 合法的语言 |
93
|
|
|
$range = $langSet; |
94
|
|
|
$this->lang->setLangSet($range); |
95
|
|
|
} else { |
96
|
|
|
$range = $this->lang->getLangSet(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $range; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 保存当前语言到Cookie |
104
|
|
|
* @access protected |
105
|
|
|
* @param Cookie $cookie Cookie对象 |
106
|
|
|
* @param string $langSet 语言 |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
protected function saveToCookie(Cookie $cookie, string $langSet) |
110
|
|
|
{ |
111
|
|
|
if ($this->config['use_cookie']) { |
112
|
|
|
$cookie->set($this->config['cookie_var'], $langSet); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.