Stop doing $that = $this in Tests
[mediawiki.git] / languages / classes / LanguageZh.php
blob51b09f694cf69bdb991fc7c1ffe9b1bb81a31618
1 <?php
2 /**
3 * Chinese specific code.
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.
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.
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
20 * @file
21 * @ingroup Language
24 /**
25 * @ingroup Language
27 class ZhConverter extends LanguageConverter {
28 /**
29 * @param Language $langobj
30 * @param string $maincode
31 * @param array $variants
32 * @param array $variantfallbacks
33 * @param array $flags
34 * @param array $manualLevel
36 function __construct( $langobj, $maincode,
37 $variants = array(),
38 $variantfallbacks = array(),
39 $flags = array(),
40 $manualLevel = array() ) {
41 $this->mDescCodeSep = ':';
42 $this->mDescVarSep = ';';
43 parent::__construct( $langobj, $maincode,
44 $variants,
45 $variantfallbacks,
46 $flags,
47 $manualLevel );
48 $names = array(
49 'zh' => '原文',
50 'zh-hans' => '简体',
51 'zh-hant' => '繁體',
52 'zh-cn' => '大陆',
53 'zh-tw' => '台灣',
54 'zh-hk' => '香港',
55 'zh-mo' => '澳門',
56 'zh-sg' => '新加坡',
57 'zh-my' => '大马',
59 $this->mVariantNames = array_merge( $this->mVariantNames, $names );
62 function loadDefaultTables() {
63 $this->mTables = array(
64 'zh-hans' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2Hans ),
65 'zh-hant' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2Hant ),
66 'zh-cn' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2CN ),
67 'zh-hk' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2HK ),
68 'zh-mo' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2HK ),
69 'zh-my' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2CN ),
70 'zh-sg' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2CN ),
71 'zh-tw' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion::$zh2TW ),
72 'zh' => new ReplacementArray
76 function postLoadTables() {
77 $this->mTables['zh-cn']->setArray(
78 $this->mTables['zh-cn']->getArray() + $this->mTables['zh-hans']->getArray()
80 $this->mTables['zh-hk']->setArray(
81 $this->mTables['zh-hk']->getArray() + $this->mTables['zh-hant']->getArray()
83 $this->mTables['zh-mo']->setArray(
84 $this->mTables['zh-mo']->getArray() + $this->mTables['zh-hant']->getArray()
86 $this->mTables['zh-my']->setArray(
87 $this->mTables['zh-my']->getArray() + $this->mTables['zh-hans']->getArray()
89 $this->mTables['zh-sg']->setArray(
90 $this->mTables['zh-sg']->getArray() + $this->mTables['zh-hans']->getArray()
92 $this->mTables['zh-tw']->setArray(
93 $this->mTables['zh-tw']->getArray() + $this->mTables['zh-hant']->getArray()
97 /**
98 * @param string $key
99 * @return string
101 function convertCategoryKey( $key ) {
102 return $this->autoConvert( $key, 'zh' );
107 * class that handles both Traditional and Simplified Chinese
108 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
110 * @ingroup Language
112 class LanguageZh extends LanguageZh_hans {
113 function __construct() {
114 global $wgHooks;
116 parent::__construct();
118 $variants = array(
119 'zh',
120 'zh-hans',
121 'zh-hant',
122 'zh-cn',
123 'zh-hk',
124 'zh-mo',
125 'zh-my',
126 'zh-sg',
127 'zh-tw'
130 $variantfallbacks = array(
131 'zh' => array( 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ),
132 'zh-hans' => array( 'zh-cn', 'zh-sg', 'zh-my' ),
133 'zh-hant' => array( 'zh-tw', 'zh-hk', 'zh-mo' ),
134 'zh-cn' => array( 'zh-hans', 'zh-sg', 'zh-my' ),
135 'zh-sg' => array( 'zh-hans', 'zh-cn', 'zh-my' ),
136 'zh-my' => array( 'zh-hans', 'zh-sg', 'zh-cn' ),
137 'zh-tw' => array( 'zh-hant', 'zh-hk', 'zh-mo' ),
138 'zh-hk' => array( 'zh-hant', 'zh-mo', 'zh-tw' ),
139 'zh-mo' => array( 'zh-hant', 'zh-hk', 'zh-tw' ),
141 $ml = array(
142 'zh' => 'disable',
143 'zh-hans' => 'unidirectional',
144 'zh-hant' => 'unidirectional',
147 $this->mConverter = new ZhConverter( $this, 'zh',
148 $variants, $variantfallbacks,
149 array(),
150 $ml );
152 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
156 * this should give much better diff info
158 * @param string $text
159 * @return string
161 function segmentForDiff( $text ) {
162 return preg_replace( '/[\xc0-\xff][\x80-\xbf]*/', ' $0', $text );
166 * @param string $text
167 * @return string
169 function unsegmentForDiff( $text ) {
170 return preg_replace( '/ ([\xc0-\xff][\x80-\xbf]*)/', '$1', $text );
174 * auto convert to zh-hans and normalize special characters.
176 * @param string $string
177 * @param string $autoVariant Defaults to 'zh-hans'
178 * @return string
180 function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
182 // always convert to zh-hans before indexing. it should be
183 // better to use zh-hans for search, since conversion from
184 // Traditional to Simplified is less ambiguous than the
185 // other way around
186 $s = $this->mConverter->autoConvert( $string, $autoVariant );
187 // LanguageZh_hans::normalizeForSearch
188 $s = parent::normalizeForSearch( $s );
189 return $s;
194 * @param array $termsArray
195 * @return array
197 function convertForSearchResult( $termsArray ) {
198 $terms = implode( '|', $termsArray );
199 $terms = self::convertDoubleWidth( $terms );
200 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
201 $ret = array_unique( explode( '|', $terms ) );
202 return $ret;