language.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * 语言调用类
  4. *
  5. * 语言调用类,为静态使用
  6. *
  7. *
  8. * @package
  9. */
  10. defined('InShopNC') or exit('Access Invalid!');
  11. final class Language
  12. {
  13. private static $language_content = array();
  14. /**
  15. * 得到数组变量的GBK编码
  16. *
  17. * @param array $key 数组
  18. * @return array 数组类型的返回结果
  19. */
  20. public static function getGBK($key)
  21. {
  22. /**
  23. * 转码
  24. */
  25. if (strtoupper(CHARSET) == 'GBK' && !empty($key))
  26. {
  27. if (is_array($key)){
  28. $result = var_export($key, true);//变为字符串
  29. $result = iconv('UTF-8','GBK',$result);
  30. eval("\$result = $result;");//转换回数组
  31. }else {
  32. $result = iconv('UTF-8','GBK',$key);
  33. }
  34. }
  35. return $result;
  36. }
  37. /**
  38. * 得到数组变量的UTF-8编码
  39. *
  40. * @param array $key GBK编码数组
  41. * @return array 数组类型的返回结果
  42. */
  43. public static function getUTF8($key)
  44. {
  45. /**
  46. * 转码
  47. */
  48. if (!empty($key))
  49. {
  50. if (is_array($key)){
  51. $result = var_export($key, true);//变为字符串
  52. $result = iconv('GBK','UTF-8',$result);
  53. eval("\$result = $result;");//转换回数组
  54. }else {
  55. $result = iconv('GBK','UTF-8',$key);
  56. }
  57. }
  58. return $result;
  59. }
  60. /**
  61. * 取指定下标的数组内容
  62. *
  63. * @param string $key 数组下标
  64. * @return string 字符串形式的返回结果
  65. */
  66. public static function get($key,$charset = '')
  67. {
  68. $result = self::$language_content[$key] ? self::$language_content[$key] : '';
  69. if (strtoupper(CHARSET) == 'UTF-8' || strtoupper($charset) == 'UTF-8') {
  70. return $result;//json格式时不转换
  71. }
  72. /**
  73. * 转码
  74. */
  75. if (strtoupper(CHARSET) == 'GBK' && !empty($result)){
  76. $result = iconv('UTF-8','GBK',$result);
  77. }
  78. return $result;
  79. }
  80. /**
  81. * 设置指定下标的数组内容
  82. *
  83. * @param string $key 数组下标
  84. * @param string $value 值
  85. * @return bool 字符串形式的返回结果
  86. */
  87. public static function set($key,$value)
  88. {
  89. self::$language_content[$key] = $value;
  90. return true;
  91. }
  92. /**
  93. * 通过语言包文件设置语言内容
  94. *
  95. * @param string $file 语言包文件,可以按照逗号(,)分隔
  96. * @return bool 布尔类型的返回结果
  97. */
  98. public static function read($file)
  99. {
  100. str_replace(',',',',$file);
  101. $tmp = explode(',',$file);
  102. foreach ($tmp as $v)
  103. {
  104. $tmp_file = BASE_PATH.'/language/'.LANG_TYPE.DS.$v.'.php';
  105. if (file_exists($tmp_file))
  106. {
  107. require($tmp_file);
  108. if (!empty($lang) && is_array($lang)) {
  109. self::$language_content = array_merge(self::$language_content,$lang);
  110. }
  111. unset($lang);
  112. }
  113. }
  114. return true;
  115. }
  116. /**
  117. * 取语言包全部内容
  118. *
  119. * @return array 数组类型的返回结果
  120. */
  121. public static function getLangContent($charset = '')
  122. {
  123. $result = self::$language_content;
  124. if (strtoupper(CHARSET) == 'UTF-8' || strtoupper($charset) == 'UTF-8') {
  125. return $result;//json格式时不转换
  126. }
  127. /**
  128. * 转码
  129. */
  130. if (strtoupper(CHARSET) == 'GBK' && !empty($result))
  131. {
  132. if (is_array($result)){
  133. foreach ($result as $k => $v){
  134. $result[$k] = iconv('UTF-8','GBK',$v);
  135. }
  136. }
  137. }
  138. return $result;
  139. }
  140. public static function appendLanguage($lang)
  141. {
  142. if (!empty($lang) && is_array($lang)){
  143. self::$language_content = array_merge(self::$language_content,$lang);
  144. }
  145. }
  146. }