123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace think;
- class Lang
- {
-
- private static $lang = [];
-
- private static $range = 'zh-cn';
-
- protected static $langDetectVar = 'lang';
-
- protected static $langCookieVar = 'think_var';
-
- protected static $langCookieExpire = 3600;
-
- protected static $allowLangList = [];
-
- protected static $acceptLanguage = ['zh-hans-cn' => 'zh-cn'];
-
- public static function range($range = '')
- {
- if ($range) {
- self::$range = $range;
- }
- return self::$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];
- }
- return self::$lang[$range][strtolower($name)] = $value;
- }
-
- public static function load($file, $range = '')
- {
- $range = $range ?: self::$range;
- $file = is_string($file) ? [$file] : $file;
- if (!isset(self::$lang[$range])) {
- self::$lang[$range] = [];
- }
- $lang = [];
- foreach ($file as $_file) {
- if (is_file($_file)) {
-
- App::$debug && Log::record('[ LANG ] ' . $_file, 'info');
- $_lang = include $_file;
- if (is_array($_lang)) {
- $lang = array_change_key_case($_lang) + $lang;
- }
- }
- }
- if (!empty($lang)) {
- self::$lang[$range] = $lang + self::$lang[$range];
- }
- return self::$lang[$range];
- }
-
- public static function has($name, $range = '')
- {
- $range = $range ?: self::$range;
- return isset(self::$lang[$range][strtolower($name)]);
- }
-
- 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, $value);
- $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()
- {
- $langSet = '';
- if (isset($_GET[self::$langDetectVar])) {
-
- $langSet = strtolower($_GET[self::$langDetectVar]);
- } elseif (isset($_COOKIE[self::$langCookieVar])) {
-
- $langSet = strtolower($_COOKIE[self::$langCookieVar]);
- } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
-
- preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
- $langSet = strtolower($matches[1] ?? '');
- $acceptLangs = Config::get('header_accept_lang');
- if (isset($acceptLangs[$langSet])) {
- $langSet = $acceptLangs[$langSet];
- } elseif (isset(self::$acceptLanguage[$langSet])) {
- $langSet = self::$acceptLanguage[$langSet];
- }
- }
- if (preg_match('/^([a-z0-9\-_]{2,10})$/i', $langSet, $matches)) {
- $langSet = strtolower($matches[1]);
- } else {
- $langSet = self::$range;
- }
-
- if (empty(self::$allowLangList) || in_array($langSet, self::$allowLangList)) {
- self::$range = $langSet ?: self::$range;
- }
- return self::$range;
- }
-
- public static function setLangDetectVar($var)
- {
- self::$langDetectVar = $var;
- }
-
- public static function setLangCookieVar($var)
- {
- self::$langCookieVar = $var;
- }
-
- public static function setLangCookieExpire($expire)
- {
- self::$langCookieExpire = $expire;
- }
-
- public static function setAllowLangList($list)
- {
- self::$allowLangList = $list;
- }
- }
|