Lang.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 = 'zh-cn';
  22. /**
  23. * @var string 语言自动侦测的变量
  24. */
  25. protected static $langDetectVar = 'lang';
  26. /**
  27. * @var string 语言 Cookie 变量
  28. */
  29. protected static $langCookieVar = 'think_var';
  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' => 'zh-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. return self::$range;
  54. }
  55. /**
  56. * 设置语言定义(不区分大小写)
  57. * @access public
  58. * @param string|array $name 语言变量
  59. * @param string $value 语言值
  60. * @param string $range 语言作用域
  61. * @return mixed
  62. */
  63. public static function set($name, $value = null, $range = '')
  64. {
  65. $range = $range ?: self::$range;
  66. if (!isset(self::$lang[$range])) {
  67. self::$lang[$range] = [];
  68. }
  69. if (is_array($name)) {
  70. return self::$lang[$range] = array_change_key_case($name) + self::$lang[$range];
  71. }
  72. return self::$lang[$range][strtolower($name)] = $value;
  73. }
  74. /**
  75. * 加载语言定义(不区分大小写)
  76. * @access public
  77. * @param array|string $file 语言文件
  78. * @param string $range 语言作用域
  79. * @return mixed
  80. */
  81. public static function load($file, $range = '')
  82. {
  83. $range = $range ?: self::$range;
  84. $file = is_string($file) ? [$file] : $file;
  85. if (!isset(self::$lang[$range])) {
  86. self::$lang[$range] = [];
  87. }
  88. $lang = [];
  89. foreach ($file as $_file) {
  90. if (is_file($_file)) {
  91. // 记录加载信息
  92. App::$debug && Log::record('[ LANG ] ' . $_file, 'info');
  93. $_lang = include $_file;
  94. if (is_array($_lang)) {
  95. $lang = array_change_key_case($_lang) + $lang;
  96. }
  97. }
  98. }
  99. if (!empty($lang)) {
  100. self::$lang[$range] = $lang + self::$lang[$range];
  101. }
  102. return self::$lang[$range];
  103. }
  104. /**
  105. * 获取语言定义(不区分大小写)
  106. * @access public
  107. * @param string|null $name 语言变量
  108. * @param string $range 语言作用域
  109. * @return mixed
  110. */
  111. public static function has($name, $range = '')
  112. {
  113. $range = $range ?: self::$range;
  114. return isset(self::$lang[$range][strtolower($name)]);
  115. }
  116. /**
  117. * 获取语言定义(不区分大小写)
  118. * @access public
  119. * @param string|null $name 语言变量
  120. * @param array $vars 变量替换
  121. * @param string $range 语言作用域
  122. * @return mixed
  123. */
  124. public static function get($name = null, $vars = [], $range = '')
  125. {
  126. $range = $range ?: self::$range;
  127. // 空参数返回所有定义
  128. if (empty($name)) {
  129. return self::$lang[$range];
  130. }
  131. $key = strtolower($name);
  132. $value = isset(self::$lang[$range][$key]) ? self::$lang[$range][$key] : $name;
  133. // 变量解析
  134. if (!empty($vars) && is_array($vars)) {
  135. /**
  136. * Notes:
  137. * 为了检测的方便,数字索引的判断仅仅是参数数组的第一个元素的key为数字0
  138. * 数字索引采用的是系统的 sprintf 函数替换,用法请参考 sprintf 函数
  139. */
  140. if (key($vars) === 0) {
  141. // 数字索引解析
  142. array_unshift($vars, $value);
  143. $value = call_user_func_array('sprintf', $vars);
  144. } else {
  145. // 关联索引解析
  146. $replace = array_keys($vars);
  147. foreach ($replace as &$v) {
  148. $v = "{:{$v}}";
  149. }
  150. $value = str_replace($replace, $vars, $value);
  151. }
  152. }
  153. return $value;
  154. }
  155. /**
  156. * 自动侦测设置获取语言选择
  157. * @access public
  158. * @return string
  159. */
  160. public static function detect()
  161. {
  162. $langSet = '';
  163. if (isset($_GET[self::$langDetectVar])) {
  164. // url 中设置了语言变量
  165. $langSet = strtolower($_GET[self::$langDetectVar]);
  166. } elseif (isset($_COOKIE[self::$langCookieVar])) {
  167. // Cookie 中设置了语言变量
  168. $langSet = strtolower($_COOKIE[self::$langCookieVar]);
  169. } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  170. // 自动侦测浏览器语言
  171. preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
  172. $langSet = strtolower($matches[1] ?? '');
  173. $acceptLangs = Config::get('header_accept_lang');
  174. if (isset($acceptLangs[$langSet])) {
  175. $langSet = $acceptLangs[$langSet];
  176. } elseif (isset(self::$acceptLanguage[$langSet])) {
  177. $langSet = self::$acceptLanguage[$langSet];
  178. }
  179. }
  180. if (preg_match('/^([a-z0-9\-_]{2,10})$/i', $langSet, $matches)) {
  181. $langSet = strtolower($matches[1]);
  182. } else {
  183. $langSet = self::$range;
  184. }
  185. // 合法的语言
  186. if (empty(self::$allowLangList) || in_array($langSet, self::$allowLangList)) {
  187. self::$range = $langSet ?: self::$range;
  188. }
  189. return self::$range;
  190. }
  191. /**
  192. * 设置语言自动侦测的变量
  193. * @access public
  194. * @param string $var 变量名称
  195. * @return void
  196. */
  197. public static function setLangDetectVar($var)
  198. {
  199. self::$langDetectVar = $var;
  200. }
  201. /**
  202. * 设置语言的 cookie 保存变量
  203. * @access public
  204. * @param string $var 变量名称
  205. * @return void
  206. */
  207. public static function setLangCookieVar($var)
  208. {
  209. self::$langCookieVar = $var;
  210. }
  211. /**
  212. * 设置语言的 cookie 的过期时间
  213. * @access public
  214. * @param string $expire 过期时间
  215. * @return void
  216. */
  217. public static function setLangCookieExpire($expire)
  218. {
  219. self::$langCookieExpire = $expire;
  220. }
  221. /**
  222. * 设置允许的语言列表
  223. * @access public
  224. * @param array $list 语言列表
  225. * @return void
  226. */
  227. public static function setAllowLangList($list)
  228. {
  229. self::$allowLangList = $list;
  230. }
  231. }