PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/languages/zh-cn/chinese.class.php

https://github.com/sahilbabu/phpb2b
PHP | 238 lines | 224 code | 14 blank | 0 comment | 63 complexity | 961d078e892c7f7189e1323247645d56 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. define('CODETABLE_DIR', LOCALE_PATH. 'tables'.DS);
  3. class Chinese {
  4. var $table = '';
  5. var $iconv_enabled = false;
  6. var $convertbig5 = false;
  7. var $unicode_table = array();
  8. var $config = array (
  9. 'SourceLang' => '',
  10. 'TargetLang' => '',
  11. 'GBtoUnicode_table' => 'gb-unicode.table',
  12. 'BIG5toUnicode_table' => 'big5-unicode.table',
  13. 'GBtoBIG5_table' => 'gb-big5.table',
  14. );
  15. function Chinese($SourceLang, $TargetLang, $ForceTable = FALSE) {
  16. $this->config['SourceLang'] = $this->_lang($SourceLang);
  17. $this->config['TargetLang'] = $this->_lang($TargetLang);
  18. if(function_exists('iconv') && $this->config['TargetLang'] != 'BIG5' && !$ForceTable) {
  19. $this->iconv_enabled = true;
  20. } else {
  21. $this->iconv_enabled = false;
  22. $this->OpenTable();
  23. }
  24. }
  25. function _lang($LangCode) {
  26. $LangCode = strtoupper($LangCode);
  27. if(substr($LangCode, 0, 2) == 'GB') {
  28. return 'GBK';
  29. } elseif(substr($LangCode, 0, 3) == 'BIG') {
  30. return 'BIG5';
  31. } elseif(substr($LangCode, 0, 3) == 'UTF') {
  32. return 'UTF-8';
  33. } elseif(substr($LangCode, 0, 3) == 'UNI') {
  34. return 'UNICODE';
  35. }
  36. }
  37. function _hex2bin($hexdata) {
  38. for($i=0; $i < strlen($hexdata); $i += 2) {
  39. $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  40. }
  41. return $bindata;
  42. }
  43. function OpenTable() {
  44. $this->unicode_table = array();
  45. if(!$this->iconv_enabled && $this->config['TargetLang'] == 'BIG5') {
  46. $this->config['TargetLang'] = 'GBK';
  47. $this->convertbig5 = TRUE;
  48. }
  49. if($this->config['SourceLang'] == 'GBK' || $this->config['TargetLang'] == 'GBK') {
  50. $this->table = CODETABLE_DIR.$this->config['GBtoUnicode_table'];
  51. } elseif($this->config['SourceLang'] == 'BIG5' || $this->config['TargetLang'] == 'BIG5') {
  52. $this->table = CODETABLE_DIR.$this->config['BIG5toUnicode_table'];
  53. }
  54. if (version_compare(PHP_VERSION, '5.0.0') >= 0) {
  55. $fp = fopen($this->table, 'rb');
  56. $tabletmp = fread($fp, filesize($this->table));
  57. }else{
  58. $tabletmp = file_get_contents($this->table);
  59. }
  60. for($i = 0; $i < strlen($tabletmp); $i += 4) {
  61. $tmp = unpack('nkey/nvalue', substr($tabletmp, $i, 4));
  62. if($this->config['TargetLang'] == 'UTF-8') {
  63. $this->unicode_table[$tmp['key']] = '0x'.dechex($tmp['value']);
  64. } elseif($this->config['SourceLang'] == 'UTF-8') {
  65. $this->unicode_table[$tmp['value']] = '0x'.dechex($tmp['key']);
  66. } elseif($this->config['TargetLang'] == 'UNICODE') {
  67. $this->unicode_table[$tmp['key']] = dechex($tmp['value']);
  68. }
  69. }
  70. }
  71. function CHSUtoUTF8($c) {
  72. $str = '';
  73. if($c < 0x80) {
  74. $str .= $c;
  75. } elseif($c < 0x800) {
  76. $str .= (0xC0 | $c >> 6);
  77. $str .= (0x80 | $c & 0x3F);
  78. } elseif($c < 0x10000) {
  79. $str .= (0xE0 | $c >> 12);
  80. $str .= (0x80 | $c >> 6 & 0x3F);
  81. $str .=( 0x80 | $c & 0x3F);
  82. } elseif($c < 0x200000) {
  83. $str .= (0xF0 | $c >> 18);
  84. $str .= (0x80 | $c >> 12 & 0x3F);
  85. $str .= (0x80 | $c >> 6 & 0x3F);
  86. $str .= (0x80 | $c & 0x3F);
  87. }
  88. return $str;
  89. }
  90. function GB2312toBIG5($c) {
  91. $f = fopen(CODETABLE_DIR.$this->config['GBtoBIG5_table'], 'r');
  92. $max=strlen($c)-1;
  93. for($i = 0;$i < $max;$i++){
  94. $h=ord($c[$i]);
  95. if($h>=160) {
  96. $l=ord($c[$i+1]);
  97. if($h==161 && $l==64){
  98. $gb=" ";
  99. } else{
  100. fseek($f,($h-160)*510+($l-1)*2);
  101. $gb=fread($f,2);
  102. }
  103. $c[$i]=$gb[0];
  104. $c[$i+1]=$gb[1];
  105. $i++;
  106. }
  107. }
  108. $result = $c;
  109. return $result;
  110. }
  111. function Convert($SourceText) {
  112. if($this->config['SourceLang'] == $this->config['TargetLang']) {
  113. return $SourceText;
  114. } elseif($this->iconv_enabled) {
  115. if($this->config['TargetLang'] <> 'UNICODE') {
  116. return iconv($this->config['SourceLang'], $this->config['TargetLang'], $SourceText);
  117. } else {
  118. $return = '';
  119. while($SourceText != '') {
  120. if(ord(substr($SourceText, 0, 1)) > 127) {
  121. $return .= "&#x".dechex($this->Utf8_Unicode(iconv($this->config['SourceLang'],"UTF-8", substr($SourceText, 0, 2)))).";";
  122. $SourceText = substr($SourceText, 2, strlen($SourceText));
  123. } else {
  124. $return .= substr($SourceText, 0, 1);
  125. $SourceText = substr($SourceText, 1, strlen($SourceText));
  126. }
  127. }
  128. return $return;
  129. }
  130. } elseif($this->config['TargetLang'] == 'UNICODE') {
  131. $utf = '';
  132. while($SourceText != '') {
  133. if(ord(substr($SourceText, 0, 1)) > 127) {
  134. if($this->config['SourceLang'] == 'GBK') {
  135. $utf .= '&#x'.$this->unicode_table[hexdec(bin2hex(substr($SourceText, 0, 2))) - 0x8080].';';
  136. } elseif($this->config['SourceLang'] == 'BIG5') {
  137. $utf .= '&#x'.$this->unicode_table[hexdec(bin2hex(substr($SourceText, 0, 2)))].';';
  138. }
  139. $SourceText = substr($SourceText, 2, strlen($SourceText));
  140. } else {
  141. $utf .= substr($SourceText, 0, 1);
  142. $SourceText = substr($SourceText, 1, strlen($SourceText));
  143. }
  144. }
  145. return $utf;
  146. } else {
  147. $ret = '';
  148. if($this->config['SourceLang'] == 'UTF-8') {
  149. $out = '';
  150. $len = strlen($SourceText);
  151. $i = 0;
  152. while($i < $len) {
  153. $c = ord(substr($SourceText, $i++, 1));
  154. switch($c >> 4) {
  155. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  156. $out .= substr($SourceText, $i - 1, 1);
  157. break;
  158. case 12: case 13:
  159. $char2 = ord(substr($SourceText, $i++, 1));
  160. $char3 = $this->unicode_table[(($c & 0x1F) << 6) | ($char2 & 0x3F)];
  161. if($this->config['TargetLang'] == 'GBK') {
  162. $out .= $this->_hex2bin(dechex($char3 + 0x8080));
  163. } elseif($this->config['TargetLang'] == 'BIG5') {
  164. $out .= $this->_hex2bin($char3);
  165. }
  166. break;
  167. case 14:
  168. $char2 = ord(substr($SourceText, $i++, 1));
  169. $char3 = ord(substr($SourceText, $i++, 1));
  170. $char4 = $this->unicode_table[(($c & 0x0F) << 12) | (($char2 & 0x3F) << 6) | (($char3 & 0x3F) << 0)];
  171. if($this->config['TargetLang'] == 'GBK') {
  172. $out .= $this->_hex2bin(dechex($char4 + 0x8080));
  173. } elseif($this->config['TargetLang'] == 'BIG5') {
  174. $out .= $this->_hex2bin($char4);
  175. }
  176. break;
  177. }
  178. }
  179. return !$this->convertbig5 ? $out : $this->GB2312toBIG5($out);
  180. } else {
  181. while($SourceText != '') {
  182. if(ord(substr($SourceText, 0, 1)) > 127) {
  183. if($this->config['SourceLang'] == 'BIG5') {
  184. $utf8 = $this->CHSUtoUTF8(hexdec($this->unicode_table[hexdec(bin2hex(substr($SourceText, 0, 2)))]));
  185. } elseif($this->config['SourceLang'] == 'GBK') {
  186. $utf8=$this->CHSUtoUTF8(hexdec($this->unicode_table[hexdec(bin2hex(substr($SourceText, 0, 2))) - 0x8080]));
  187. }
  188. for($i = 0; $i < strlen($utf8); $i += 3) {
  189. $ret .= chr(substr($utf8, $i, 3));
  190. }
  191. $SourceText = substr($SourceText, 2, strlen($SourceText));
  192. } else {
  193. $ret .= substr($SourceText, 0, 1);
  194. $SourceText = substr($SourceText, 1, strlen($SourceText));
  195. }
  196. }
  197. $SourceText = '';
  198. return $ret;
  199. }
  200. }
  201. }
  202. function Utf8_Unicode($char) {
  203. switch(strlen($char)) {
  204. case 1:
  205. return ord($char);
  206. case 2:
  207. $n = (ord($char[0]) & 0x3f) << 6;
  208. $n += ord($char[1]) & 0x3f;
  209. return $n;
  210. case 3:
  211. $n = (ord($char[0]) & 0x1f) << 12;
  212. $n += (ord($char[1]) & 0x3f) << 6;
  213. $n += ord($char[2]) & 0x3f;
  214. return $n;
  215. case 4:
  216. $n = (ord($char[0]) & 0x0f) << 18;
  217. $n += (ord($char[1]) & 0x3f) << 12;
  218. $n += (ord($char[2]) & 0x3f) << 6;
  219. $n += ord($char[3]) & 0x3f;
  220. return $n;
  221. }
  222. }
  223. }
  224. ?>