LanguageZh.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. require_once( dirname(__FILE__).'/../LanguageConverter.php' );
  3. require_once( dirname(__FILE__).'/LanguageZh_hans.php' );
  4. /**
  5. * @ingroup Language
  6. */
  7. class ZhConverter extends LanguageConverter {
  8. function __construct($langobj, $maincode,
  9. $variants=array(),
  10. $variantfallbacks=array(),
  11. $markup=array(),
  12. $flags = array(),
  13. $manualLevel = array() ) {
  14. $this->mDescCodeSep = ':';
  15. $this->mDescVarSep = ';';
  16. parent::__construct($langobj, $maincode,
  17. $variants,
  18. $variantfallbacks,
  19. $markup,
  20. $flags,
  21. $manualLevel);
  22. $names = array(
  23. 'zh' => '原文',
  24. 'zh-hans' => '简体',
  25. 'zh-hant' => '繁體',
  26. 'zh-cn' => '大陆',
  27. 'zh-tw' => '台灣',
  28. 'zh-hk' => '香港',
  29. 'zh-mo' => '澳門',
  30. 'zh-sg' => '新加坡',
  31. 'zh-my' => '大马',
  32. );
  33. $this->mVariantNames = array_merge($this->mVariantNames,$names);
  34. $this->loadNamespaceTables();
  35. }
  36. function loadNamespaceTables() {
  37. global $wgMetaNamespace;
  38. $nsproject = $wgMetaNamespace;
  39. $projecttable = array(
  40. 'Wikipedia' => '维基百科',
  41. 'Wikisource' => '维基文库',
  42. 'Wikinews' => '维基新闻',
  43. 'Wiktionary' => '维基词典',
  44. 'Wikibooks' => '维基教科书',
  45. 'Wikiquote' => '维基语录',
  46. );
  47. $this->mNamespaceTables['zh-hans'] = array(
  48. 'Media' => '媒体',
  49. 'Special' => '特殊',
  50. 'Talk' => '讨论',
  51. 'User' => '用户',
  52. 'User talk' => '用户讨论',
  53. $nsproject
  54. => isset($projecttable[$nsproject]) ?
  55. $projecttable[$nsproject] : $nsproject,
  56. $nsproject . ' talk'
  57. => isset($projecttable[$nsproject]) ?
  58. $projecttable[$nsproject] . '讨论' : $nsproject . '讨论',
  59. 'File' => '文件',
  60. 'File talk' => '文件讨论',
  61. 'MediaWiki' => 'MediaWiki',
  62. 'MediaWiki talk' => 'MediaWiki讨论',
  63. 'Template' => '模板',
  64. 'Template talk' => '模板讨论',
  65. 'Help' => '帮助',
  66. 'Help talk' => '帮助讨论',
  67. 'Category' => '分类',
  68. 'Category talk' => '分类讨论',
  69. );
  70. $this->mNamespaceTables['zh-hant'] = array_merge($this->mNamespaceTables['zh-hans']);
  71. $this->mNamespaceTables['zh-hant']['File'] = '檔案';
  72. $this->mNamespaceTables['zh-hant']['File talk'] = '檔案討論';
  73. $this->mNamespaceTables['zh'] = array_merge($this->mNamespaceTables['zh-hans']);
  74. $this->mNamespaceTables['zh-cn'] = array_merge($this->mNamespaceTables['zh-hans']);
  75. $this->mNamespaceTables['zh-hk'] = array_merge($this->mNamespaceTables['zh-hant']);
  76. $this->mNamespaceTables['zh-mo'] = array_merge($this->mNamespaceTables['zh-hant']);
  77. $this->mNamespaceTables['zh-my'] = array_merge($this->mNamespaceTables['zh-hans']);
  78. $this->mNamespaceTables['zh-sg'] = array_merge($this->mNamespaceTables['zh-hans']);
  79. $this->mNamespaceTables['zh-tw'] = array_merge($this->mNamespaceTables['zh-hant']);
  80. }
  81. function loadDefaultTables() {
  82. require( dirname(__FILE__)."/../../includes/ZhConversion.php" );
  83. $this->mTables = array(
  84. 'zh-hans' => new ReplacementArray( $zh2Hans ),
  85. 'zh-hant' => new ReplacementArray( $zh2Hant ),
  86. 'zh-cn' => new ReplacementArray( array_merge($zh2Hans, $zh2CN) ),
  87. 'zh-hk' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
  88. 'zh-mo' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
  89. 'zh-my' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
  90. 'zh-sg' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
  91. 'zh-tw' => new ReplacementArray( array_merge($zh2Hant, $zh2TW) ),
  92. 'zh' => new ReplacementArray
  93. );
  94. }
  95. function postLoadTables() {
  96. $this->mTables['zh-cn']->merge( $this->mTables['zh-hans'] );
  97. $this->mTables['zh-hk']->merge( $this->mTables['zh-hant'] );
  98. $this->mTables['zh-mo']->merge( $this->mTables['zh-hant'] );
  99. $this->mTables['zh-my']->merge( $this->mTables['zh-hans'] );
  100. $this->mTables['zh-sg']->merge( $this->mTables['zh-hans'] );
  101. $this->mTables['zh-tw']->merge( $this->mTables['zh-hant'] );
  102. }
  103. /* there shouldn't be any latin text in Chinese conversion, so no need
  104. to mark anything.
  105. $noParse is there for compatibility with LanguageConvert::markNoConversion
  106. */
  107. function markNoConversion($text, $noParse = false) {
  108. return $text;
  109. }
  110. function convertCategoryKey( $key ) {
  111. return $this->autoConvert( $key, 'zh' );
  112. }
  113. }
  114. /**
  115. * class that handles both Traditional and Simplified Chinese
  116. * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
  117. *
  118. * @ingroup Language
  119. */
  120. class LanguageZh extends LanguageZh_hans {
  121. function __construct() {
  122. global $wgHooks;
  123. parent::__construct();
  124. $variants = array('zh','zh-hans','zh-hant','zh-cn','zh-hk','zh-mo','zh-my','zh-sg','zh-tw');
  125. $variantfallbacks = array(
  126. 'zh' => array('zh-hans','zh-hant','zh-cn','zh-tw','zh-hk','zh-sg','zh-mo','zh-my'),
  127. 'zh-hans' => array('zh-cn','zh-sg','zh-my'),
  128. 'zh-hant' => array('zh-tw','zh-hk','zh-mo'),
  129. 'zh-cn' => array('zh-hans','zh-sg','zh-my'),
  130. 'zh-sg' => array('zh-hans','zh-cn','zh-my'),
  131. 'zh-my' => array('zh-hans','zh-sg','zh-cn'),
  132. 'zh-tw' => array('zh-hant','zh-hk','zh-mo'),
  133. 'zh-hk' => array('zh-hant','zh-mo','zh-tw'),
  134. 'zh-mo' => array('zh-hant','zh-hk','zh-tw'),
  135. );
  136. $ml=array(
  137. 'zh' => 'disable',
  138. 'zh-hans' => 'unidirectional',
  139. 'zh-hant' => 'unidirectional',
  140. );
  141. $this->mConverter = new ZhConverter( $this, 'zh',
  142. $variants, $variantfallbacks,
  143. array(),array(),
  144. $ml);
  145. $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
  146. }
  147. # this should give much better diff info
  148. function segmentForDiff( $text ) {
  149. return preg_replace(
  150. "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
  151. "' ' .\"$1\"", $text);
  152. }
  153. function unsegmentForDiff( $text ) {
  154. return preg_replace(
  155. "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
  156. "\"$1\"", $text);
  157. }
  158. // word segmentation
  159. function stripForSearch( $string ) {
  160. wfProfileIn( __METHOD__ );
  161. // eventually this should be a word segmentation
  162. // for now just treat each character as a word
  163. // @fixme only do this for Han characters...
  164. $t = preg_replace(
  165. "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
  166. " $1", $string);
  167. //always convert to zh-hans before indexing. it should be
  168. //better to use zh-hans for search, since conversion from
  169. //Traditional to Simplified is less ambiguous than the
  170. //other way around
  171. $t = $this->mConverter->autoConvert($t, 'zh-hans');
  172. $t = parent::stripForSearch( $t );
  173. wfProfileOut( __METHOD__ );
  174. return $t;
  175. }
  176. function convertForSearchResult( $termsArray ) {
  177. $terms = implode( '|', $termsArray );
  178. $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
  179. $ret = array_unique( explode('|', $terms) );
  180. return $ret;
  181. }
  182. }