Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. class Lang
  13. {
  14. /**
  15. * @var array 语言数据
  16. */
  17. private static $lang = [];
  18. /**
  19. * @var string 语言作用域
  20. */
  21. private static $range = 'cn';
  22. /**
  23. * @var string 语言自动侦测的变量
  24. */
  25. protected static $langDetectVar = 'lang';
  26. /**
  27. * @var string 语言 Cookie 变量
  28. */
  29. protected static $langCookieVar = 'home_lang';
  30. /**
  31. * @var int 语言 Cookie 的过期时间
  32. */
  33. protected static $langCookieExpire = 3600;
  34. /**
  35. * @var array 允许语言列表
  36. */
  37. protected static $allowLangList = [];
  38. /**
  39. * @var array Accept-Language 转义为对应语言包名称 系统默认配置
  40. */
  41. protected static $acceptLanguage = ['zh-hans-cn' => 'cn'];
  42. /**
  43. * 设定当前的语言
  44. * @access public
  45. * @param string $range 语言作用域
  46. * @return string
  47. */
  48. public static function range($range = '')
  49. {
  50. if ($range) {
  51. self::$range = $range;
  52. }
  53. $range = trim(self::$range, '/'); // 处理语言变量值后面多一个斜杠 by 小虎哥
  54. return self::$range;
  55. }
  56. /**
  57. * 设置语言定义(不区分大小写)
  58. * @access public
  59. * @param string|array $name 语言变量
  60. * @param string $value 语言值
  61. * @param string $range 语言作用域
  62. * @return mixed
  63. */
  64. public static function set($name, $value = null, $range = '')
  65. {
  66. $range = $range ?: self::$range;
  67. if (!isset(self::$lang[$range])) {
  68. self::$lang[$range] = [];
  69. }
  70. if (is_array($name)) {
  71. return self::$lang[$range] = array_change_key_case($name) + self::$lang[$range];
  72. }
  73. return self::$lang[$range][strtolower($name)] = $value;
  74. }
  75. /**
  76. * 加载语言定义(不区分大小写)
  77. * @access public
  78. * @param array|string $file 语言文件
  79. * @param string $range 语言作用域
  80. * @return mixed
  81. */
  82. public static function load($file, $range = '')
  83. {
  84. $range = $range ?: self::$range;
  85. $file = is_string($file) ? [$file] : $file;
  86. if (!isset(self::$lang[$range])) {
  87. self::$lang[$range] = [];
  88. }
  89. $lang = [];
  90. foreach ($file as $_file) {
  91. if (is_file($_file)) {
  92. // 记录加载信息
  93. App::$debug && Log::record('[ LANG ] ' . $_file, 'info');
  94. $_lang = include $_file;
  95. if (is_array($_lang)) {
  96. $lang = array_change_key_case($_lang) + $lang;
  97. }
  98. }
  99. }
  100. if (!empty($lang)) {
  101. self::$lang[$range] = $lang + self::$lang[$range];
  102. }
  103. return self::$lang[$range];
  104. }
  105. /**
  106. * 获取语言定义(不区分大小写)
  107. * @access public
  108. * @param string|null $name 语言变量
  109. * @param string $range 语言作用域
  110. * @return mixed
  111. */
  112. public static function has($name, $range = '')
  113. {
  114. $range = $range ?: self::$range;
  115. return isset(self::$lang[$range][strtolower($name)]);
  116. }
  117. /**
  118. * 获取语言定义(不区分大小写)
  119. * @access public
  120. * @param string|null $name 语言变量
  121. * @param array $vars 变量替换
  122. * @param string $range 语言作用域
  123. * @return mixed
  124. */
  125. public static function get($name = null, $vars = [], $range = '')
  126. {
  127. $range = $range ?: self::$range;
  128. // 空参数返回所有定义
  129. if (empty($name)) {
  130. return self::$lang[$range];
  131. }
  132. $key = strtolower($name);
  133. // $value = isset(self::$lang[$range][$key]) ? self::$lang[$range][$key] : $name;
  134. $value = isset(self::$lang[$range][$key]) ? self::$lang[$range][$key] : ''; // 多语言 by 小虎哥
  135. // 变量解析
  136. if (!empty($vars) && is_array($vars)) {
  137. /**
  138. * Notes:
  139. * 为了检测的方便,数字索引的判断仅仅是参数数组的第一个元素的key为数字0
  140. * 数字索引采用的是系统的 sprintf 函数替换,用法请参考 sprintf 函数
  141. */
  142. if (key($vars) === 0) {
  143. // 数字索引解析
  144. array_unshift($vars, $value);
  145. $value = call_user_func_array('sprintf', $vars);
  146. } else {
  147. // 关联索引解析
  148. $replace = array_keys($vars);
  149. foreach ($replace as &$v) {
  150. $v = "{:{$v}}";
  151. }
  152. $value = str_replace($replace, $vars, $value);
  153. }
  154. }
  155. return $value;
  156. }
  157. /**
  158. * 自动侦测设置获取语言选择
  159. * @access public
  160. * @return string
  161. */
  162. public static function detect()
  163. {
  164. // 自动侦测设置获取语言选择
  165. $langSet = '';
  166. if (isset($_GET[self::$langDetectVar])) {
  167. // url 中设置了语言变量
  168. $langSet = strtolower($_GET[self::$langDetectVar]);
  169. } elseif (isset($_COOKIE[self::$langCookieVar])) {
  170. // Cookie 中设置了语言变量
  171. $langSet = strtolower($_COOKIE[self::$langCookieVar]);
  172. } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  173. // 自动侦测浏览器语言
  174. preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
  175. $langSet = strtolower($matches[1]);
  176. $acceptLangs = Config::get('header_accept_lang');
  177. if (isset($acceptLangs[$langSet])) {
  178. $langSet = $acceptLangs[$langSet];
  179. } elseif (isset(self::$acceptLanguage[$langSet])) {
  180. $langSet = self::$acceptLanguage[$langSet];
  181. }
  182. }
  183. // 合法的语言
  184. if (empty(self::$allowLangList) || in_array($langSet, self::$allowLangList)) {
  185. self::$range = $langSet ?: self::$range;
  186. }
  187. return self::$range;
  188. }
  189. /**
  190. * 设置语言自动侦测的变量
  191. * @access public
  192. * @param string $var 变量名称
  193. * @return void
  194. */
  195. public static function setLangDetectVar($var)
  196. {
  197. self::$langDetectVar = $var;
  198. }
  199. /**
  200. * 设置语言的 cookie 保存变量
  201. * @access public
  202. * @param string $var 变量名称
  203. * @return void
  204. */
  205. public static function setLangCookieVar($var)
  206. {
  207. self::$langCookieVar = $var;
  208. }
  209. /**
  210. * 设置语言的 cookie 的过期时间
  211. * @access public
  212. * @param string $expire 过期时间
  213. * @return void
  214. */
  215. public static function setLangCookieExpire($expire)
  216. {
  217. self::$langCookieExpire = $expire;
  218. }
  219. /**
  220. * 设置允许的语言列表
  221. * @access public
  222. * @param array $list 语言列表
  223. * @return void
  224. */
  225. public static function setAllowLangList($list)
  226. {
  227. self::$allowLangList = $list;
  228. }
  229. }