优化写法

This commit is contained in:
thinkphp
2015-12-21 18:00:15 +08:00
parent 4d735fb03e
commit ab5f408e50
2 changed files with 14 additions and 12 deletions

View File

@@ -110,18 +110,20 @@ class Lang
public static function detect()
{
// 自动侦测设置获取语言选择
if (isset($_GET[Config::get('lang_detect_var')])) {
$langCookieVar = Config::get('lang_cookie_var');
$langDetectVar = Config::get('lang_detect_var');
if (isset($_GET[$langDetectVar])) {
// url中设置了语言变量
$langSet = strtolower($_GET[Config::get('lang_detect_var')]);
\think\Cookie::set(Config::get('lang_cookie_var'), $langSet, 3600);
} elseif (\think\Cookie::get(Config::get('lang_cookie_var'))) {
$langSet = strtolower($_GET[$langDetectVar]);
\think\Cookie::set($langCookieVar, $langSet, 3600);
} elseif (\think\Cookie::get($langCookieVar)) {
// 获取上次用户的选择
$langSet = strtolower(\think\Cookie::get(Config::get('lang_cookie_var')));
$langSet = strtolower(\think\Cookie::get($langCookieVar));
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// 自动侦测浏览器语言
preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
$langSet = strtolower($matches[1]);
\think\Cookie::set(Config::get('lang_cookie_var'), $langSet, 3600);
\think\Cookie::set($langCookieVar, $langSet, 3600);
}
if (in_array($langSet, \think\Config::get('lang_list'))) {
// 合法的语言