LanguageZh.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Chinese specific code.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Language
  22. */
  23. /**
  24. * @ingroup Language
  25. */
  26. class ZhConverter extends LanguageConverter {
  27. /**
  28. * @param Language $langobj
  29. * @param string $maincode
  30. * @param array $variants
  31. * @param array $variantfallbacks
  32. * @param array $flags
  33. * @param array $manualLevel
  34. */
  35. function __construct( $langobj, $maincode,
  36. $variants = [],
  37. $variantfallbacks = [],
  38. $flags = [],
  39. $manualLevel = [] ) {
  40. $this->mDescCodeSep = ':';
  41. $this->mDescVarSep = ';';
  42. parent::__construct( $langobj, $maincode,
  43. $variants,
  44. $variantfallbacks,
  45. $flags,
  46. $manualLevel );
  47. $names = [
  48. 'zh' => '原文',
  49. 'zh-hans' => '简体',
  50. 'zh-hant' => '繁體',
  51. 'zh-cn' => '大陆',
  52. 'zh-tw' => '台灣',
  53. 'zh-hk' => '香港',
  54. 'zh-mo' => '澳門',
  55. 'zh-sg' => '新加坡',
  56. 'zh-my' => '大马',
  57. ];
  58. $this->mVariantNames = array_merge( $this->mVariantNames, $names );
  59. }
  60. function loadDefaultTables() {
  61. $this->mTables = [
  62. 'zh-hans' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2Hans ),
  63. 'zh-hant' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2Hant ),
  64. 'zh-cn' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2CN ),
  65. 'zh-hk' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2HK ),
  66. 'zh-mo' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2HK ),
  67. 'zh-my' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2CN ),
  68. 'zh-sg' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2CN ),
  69. 'zh-tw' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2TW ),
  70. 'zh' => new ReplacementArray
  71. ];
  72. }
  73. function postLoadTables() {
  74. $this->mTables['zh-cn']->setArray(
  75. $this->mTables['zh-cn']->getArray() + $this->mTables['zh-hans']->getArray()
  76. );
  77. $this->mTables['zh-hk']->setArray(
  78. $this->mTables['zh-hk']->getArray() + $this->mTables['zh-hant']->getArray()
  79. );
  80. $this->mTables['zh-mo']->setArray(
  81. $this->mTables['zh-mo']->getArray() + $this->mTables['zh-hant']->getArray()
  82. );
  83. $this->mTables['zh-my']->setArray(
  84. $this->mTables['zh-my']->getArray() + $this->mTables['zh-hans']->getArray()
  85. );
  86. $this->mTables['zh-sg']->setArray(
  87. $this->mTables['zh-sg']->getArray() + $this->mTables['zh-hans']->getArray()
  88. );
  89. $this->mTables['zh-tw']->setArray(
  90. $this->mTables['zh-tw']->getArray() + $this->mTables['zh-hant']->getArray()
  91. );
  92. }
  93. /**
  94. * @param string $key
  95. * @return string
  96. */
  97. function convertCategoryKey( $key ) {
  98. return $this->autoConvert( $key, 'zh' );
  99. }
  100. }
  101. /**
  102. * class that handles both Traditional and Simplified Chinese
  103. * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
  104. *
  105. * @ingroup Language
  106. */
  107. class LanguageZh extends LanguageZh_hans {
  108. function __construct() {
  109. parent::__construct();
  110. $variants = [
  111. 'zh',
  112. 'zh-hans',
  113. 'zh-hant',
  114. 'zh-cn',
  115. 'zh-hk',
  116. 'zh-mo',
  117. 'zh-my',
  118. 'zh-sg',
  119. 'zh-tw'
  120. ];
  121. $variantfallbacks = [
  122. 'zh' => [ 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ],
  123. 'zh-hans' => [ 'zh-cn', 'zh-sg', 'zh-my' ],
  124. 'zh-hant' => [ 'zh-tw', 'zh-hk', 'zh-mo' ],
  125. 'zh-cn' => [ 'zh-hans', 'zh-sg', 'zh-my' ],
  126. 'zh-sg' => [ 'zh-hans', 'zh-cn', 'zh-my' ],
  127. 'zh-my' => [ 'zh-hans', 'zh-sg', 'zh-cn' ],
  128. 'zh-tw' => [ 'zh-hant', 'zh-hk', 'zh-mo' ],
  129. 'zh-hk' => [ 'zh-hant', 'zh-mo', 'zh-tw' ],
  130. 'zh-mo' => [ 'zh-hant', 'zh-hk', 'zh-tw' ],
  131. ];
  132. $ml = [
  133. 'zh' => 'disable',
  134. 'zh-hans' => 'unidirectional',
  135. 'zh-hant' => 'unidirectional',
  136. ];
  137. $this->mConverter = new ZhConverter( $this, 'zh',
  138. $variants, $variantfallbacks,
  139. [],
  140. $ml );
  141. }
  142. /**
  143. * this should give much better diff info
  144. *
  145. * @param string $text
  146. * @return string
  147. */
  148. function segmentForDiff( $text ) {
  149. return preg_replace( '/[\xc0-\xff][\x80-\xbf]*/', ' $0', $text );
  150. }
  151. /**
  152. * @param string $text
  153. * @return string
  154. */
  155. function unsegmentForDiff( $text ) {
  156. return preg_replace( '/ ([\xc0-\xff][\x80-\xbf]*)/', '$1', $text );
  157. }
  158. /**
  159. * auto convert to zh-hans and normalize special characters.
  160. *
  161. * @param string $string
  162. * @param string $autoVariant Defaults to 'zh-hans'
  163. * @return string
  164. */
  165. function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
  166. // always convert to zh-hans before indexing. it should be
  167. // better to use zh-hans for search, since conversion from
  168. // Traditional to Simplified is less ambiguous than the
  169. // other way around
  170. $s = $this->mConverter->autoConvert( $string, $autoVariant );
  171. // LanguageZh_hans::normalizeForSearch
  172. $s = parent::normalizeForSearch( $s );
  173. return $s;
  174. }
  175. /**
  176. * @param array $termsArray
  177. * @return array
  178. */
  179. function convertForSearchResult( $termsArray ) {
  180. $terms = implode( '|', $termsArray );
  181. $terms = self::convertDoubleWidth( $terms );
  182. $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
  183. $ret = array_unique( explode( '|', $terms ) );
  184. return $ret;
  185. }
  186. }