Skip to content

Commit

Permalink
删除废弃方法 优化多语言检测
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Sep 14, 2022
1 parent 88ad731 commit c4acb8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 67 deletions.
54 changes: 0 additions & 54 deletions src/think/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,58 +287,4 @@ public function get(string $name = null, array $vars = [], string $range = '')
return $value;
}

/**
* 自动侦测设置获取语言选择
* @deprecated
* @access public
* @param Request $request
* @return string
*/
public function detect(Request $request): string
{
// 自动侦测设置获取语言选择
$langSet = '';

if ($request->get($this->config['detect_var'])) {
// url中设置了语言变量
$langSet = strtolower($request->get($this->config['detect_var']));
} elseif ($request->header($this->config['header_var'])) {
// Header中设置了语言变量
$langSet = strtolower($request->header($this->config['header_var']));
} elseif ($request->cookie($this->config['cookie_var'])) {
// Cookie中设置了语言变量
$langSet = strtolower($request->cookie($this->config['cookie_var']));
} elseif ($request->server('HTTP_ACCEPT_LANGUAGE')) {
// 自动侦测浏览器语言
$match = preg_match('/^([a-z\d\-]+)/i', $request->server('HTTP_ACCEPT_LANGUAGE'), $matches);
if ($match) {
$langSet = strtolower($matches[1]);
if (isset($this->config['accept_language'][$langSet])) {
$langSet = $this->config['accept_language'][$langSet];
}
}
}

if (empty($this->config['allow_lang_list']) || in_array($langSet, $this->config['allow_lang_list'])) {
// 合法的语言
$this->range = $langSet;
}

return $this->range;
}

/**
* 保存当前语言到Cookie
* @deprecated
* @access public
* @param Cookie $cookie Cookie对象
* @return void
*/
public function saveToCookie(Cookie $cookie)
{
if ($this->config['use_cookie']) {
$cookie->set($this->config['cookie_var'], $this->range);
}
}

}
26 changes: 13 additions & 13 deletions src/think/middleware/LoadLangPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,33 @@ protected function detect(Request $request): string

if ($request->get($this->config['detect_var'])) {
// url中设置了语言变量
$langSet = strtolower($request->get($this->config['detect_var']));
$langSet = $request->get($this->config['detect_var']);
} elseif ($request->header($this->config['header_var'])) {
// Header中设置了语言变量
$langSet = strtolower($request->header($this->config['header_var']));
$langSet = $request->header($this->config['header_var']);
} elseif ($request->cookie($this->config['cookie_var'])) {
// Cookie中设置了语言变量
$langSet = strtolower($request->cookie($this->config['cookie_var']));
$langSet = $request->cookie($this->config['cookie_var']);
} elseif ($request->server('HTTP_ACCEPT_LANGUAGE')) {
// 自动侦测浏览器语言
$match = preg_match('/^([a-z\d\-]+)/i', $request->server('HTTP_ACCEPT_LANGUAGE'), $matches);
if ($match) {
$langSet = strtolower($matches[1]);
if (isset($this->config['accept_language'][$langSet])) {
$langSet = $this->config['accept_language'][$langSet];
}
$langSet = $request->server('HTTP_ACCEPT_LANGUAGE');
}

if (preg_match('/^([a-z\d\-]+)/i', $langSet, $matches)) {
$langSet = strtolower($matches[1]);
if (isset($this->config['accept_language'][$langSet])) {
$langSet = $this->config['accept_language'][$langSet];
}
}

if (empty($this->config['allow_lang_list']) || in_array($langSet, $this->config['allow_lang_list'])) {
// 合法的语言
$range = $langSet;
$this->lang->setLangSet($range);
$this->lang->setLangSet($langSet);
} else {
$range = $this->lang->getLangSet();
$langSet = $this->lang->getLangSet();
}

return $range;
return $langSet;
}

/**
Expand Down

0 comments on commit c4acb8b

Please sign in to comment.