💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] * * * * * ## 1 语言包管理文件源代码(thinkphp\library\think\Lang.php) ~~~ private static $lang = []; private static $range = 'zh-cn'; ~~~ ~~~ public static function range($range = '') { if ('' == $range) { return self::$range; } else { self::$range = $range; } } ~~~ ~~~ public static function set($name, $value = null, $range = '') { $range = $range ?: self::$range; if (!isset(self::$lang[$range])) { self::$lang[$range] = []; } if (is_array($name)) { return self::$lang[$range] = array_change_key_case($name) + self::$lang[$range]; } else { return self::$lang[$range][strtolower($name)] = $value; } } ~~~ ~~~ public static function load($file, $range = '') { $range = $range ?: self::$range; if (!isset(self::$lang[$range])) { self::$lang[$range] = []; } if (is_string($file)) { $file = [$file]; } $lang = []; foreach ($file as $_file) { APP_DEBUG && Log::record('[ LANG ] ' . $_file, 'info'); $_lang = is_file($_file) ? include $_file : []; $lang = array_change_key_case($_lang) + $lang; } if (!empty($lang)) { self::$lang[$range] = $lang + self::$lang[$range]; } return self::$lang[$range]; } ~~~ ~~~ public static function get($name = null, $vars = [], $range = '') { $range = $range ?: self::$range; if (empty($name)) { return self::$lang[$range]; } $key = strtolower($name); $value = isset(self::$lang[$range][$key]) ? self::$lang[$range][$key] : $name; if (!empty($vars) && is_array($vars)) { if (key($vars) === 0) { array_unshift($vars, $name); $value = call_user_func_array('sprintf', $vars); } else { $replace = array_keys($vars); foreach ($replace as &$v) { $v = "{:{$v}}"; } $value = str_replace($replace, $vars, $value); } } return $value; } ~~~ ~~~ public static function detect() { $langCookieVar = Config::get('lang_cookie_var'); $langDetectVar = Config::get('lang_detect_var'); $langSet = ''; if (isset($_GET[$langDetectVar])) { $langSet = strtolower($_GET[$langDetectVar]); \think\Cookie::set($langCookieVar, $langSet, 3600); } elseif (\think\Cookie::get($langCookieVar)) { $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($langCookieVar, $langSet, 3600); } if (in_array($langSet, \think\Config::get('lang_list'))) { self::$range = $langSet; } } ~~~ ## 2 文件分析 1 静态变量 ~~~ $lange: 语言参数数组 $range: 作用域参数 ~~~ 2 `public static function range($range = ''){}` 设置语言作用域 > $range:作用域参数 3 `public static function set($name, $value = null, $range = ''){}` 设置语言配置参数 > $name:语言名称 > $value:语言对应值 > $range:作用域 4 `public static function load($file, $range = ''){}` 加载应用文件 > $file:语言文件 5 `public static function get($name = null, $vars = [], $range = ''){}` 获取语言配置参数 > $name: 语言名称 > $vars: 语言对应值 > $range: 作用域 6 `public static function detect(){}` 自动侦测设置获取语言选择 ## 3 使用 ## 4 总结