language.php 3.3 KB

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