Don't modify $wgHooks on language object construction
[lhc/web/wiklou.git] / languages / classes / LanguageZh.php
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 require_once __DIR__ . '/../LanguageConverter.php';
25 require_once __DIR__ . '/LanguageZh_hans.php';
26
27 /**
28 * @ingroup Language
29 */
30 class ZhConverter extends LanguageConverter {
31 /**
32 * @param Language $langobj
33 * @param string $maincode
34 * @param array $variants
35 * @param array $variantfallbacks
36 * @param array $flags
37 * @param array $manualLevel
38 */
39 function __construct( $langobj, $maincode,
40 $variants = array(),
41 $variantfallbacks = array(),
42 $flags = array(),
43 $manualLevel = array() ) {
44 $this->mDescCodeSep = ':';
45 $this->mDescVarSep = ';';
46 parent::__construct( $langobj, $maincode,
47 $variants,
48 $variantfallbacks,
49 $flags,
50 $manualLevel );
51 $names = array(
52 'zh' => '原文',
53 'zh-hans' => '简体',
54 'zh-hant' => '繁體',
55 'zh-cn' => '大陆',
56 'zh-tw' => '台灣',
57 'zh-hk' => '香港',
58 'zh-mo' => '澳門',
59 'zh-sg' => '新加坡',
60 'zh-my' => '大马',
61 );
62 $this->mVariantNames = array_merge( $this->mVariantNames, $names );
63 }
64
65 function loadDefaultTables() {
66 require __DIR__ . "/../../includes/ZhConversion.php";
67 $this->mTables = array(
68 'zh-hans' => new ReplacementArray( $zh2Hans ),
69 'zh-hant' => new ReplacementArray( $zh2Hant ),
70 'zh-cn' => new ReplacementArray( $zh2CN ),
71 'zh-hk' => new ReplacementArray( $zh2HK ),
72 'zh-mo' => new ReplacementArray( $zh2HK ),
73 'zh-my' => new ReplacementArray( $zh2CN ),
74 'zh-sg' => new ReplacementArray( $zh2CN ),
75 'zh-tw' => new ReplacementArray( $zh2TW ),
76 'zh' => new ReplacementArray
77 );
78 }
79
80 function postLoadTables() {
81 $this->mTables['zh-cn']->setArray(
82 $this->mTables['zh-cn']->getArray() + $this->mTables['zh-hans']->getArray()
83 );
84 $this->mTables['zh-hk']->setArray(
85 $this->mTables['zh-hk']->getArray() + $this->mTables['zh-hant']->getArray()
86 );
87 $this->mTables['zh-mo']->setArray(
88 $this->mTables['zh-mo']->getArray() + $this->mTables['zh-hant']->getArray()
89 );
90 $this->mTables['zh-my']->setArray(
91 $this->mTables['zh-my']->getArray() + $this->mTables['zh-hans']->getArray()
92 );
93 $this->mTables['zh-sg']->setArray(
94 $this->mTables['zh-sg']->getArray() + $this->mTables['zh-hans']->getArray()
95 );
96 $this->mTables['zh-tw']->setArray(
97 $this->mTables['zh-tw']->getArray() + $this->mTables['zh-hant']->getArray()
98 );
99 }
100
101 /**
102 * @param string $key
103 * @return string
104 */
105 function convertCategoryKey( $key ) {
106 return $this->autoConvert( $key, 'zh' );
107 }
108 }
109
110 /**
111 * class that handles both Traditional and Simplified Chinese
112 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
113 *
114 * @ingroup Language
115 */
116 class LanguageZh extends LanguageZh_hans {
117 function __construct() {
118 parent::__construct();
119
120 $variants = array(
121 'zh',
122 'zh-hans',
123 'zh-hant',
124 'zh-cn',
125 'zh-hk',
126 'zh-mo',
127 'zh-my',
128 'zh-sg',
129 'zh-tw'
130 );
131
132 $variantfallbacks = array(
133 'zh' => array( 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ),
134 'zh-hans' => array( 'zh-cn', 'zh-sg', 'zh-my' ),
135 'zh-hant' => array( 'zh-tw', 'zh-hk', 'zh-mo' ),
136 'zh-cn' => array( 'zh-hans', 'zh-sg', 'zh-my' ),
137 'zh-sg' => array( 'zh-hans', 'zh-cn', 'zh-my' ),
138 'zh-my' => array( 'zh-hans', 'zh-sg', 'zh-cn' ),
139 'zh-tw' => array( 'zh-hant', 'zh-hk', 'zh-mo' ),
140 'zh-hk' => array( 'zh-hant', 'zh-mo', 'zh-tw' ),
141 'zh-mo' => array( 'zh-hant', 'zh-hk', 'zh-tw' ),
142 );
143 $ml = array(
144 'zh' => 'disable',
145 'zh-hans' => 'unidirectional',
146 'zh-hant' => 'unidirectional',
147 );
148
149 $this->mConverter = new ZhConverter( $this, 'zh',
150 $variants, $variantfallbacks,
151 array(),
152 $ml );
153 }
154
155 /**
156 * this should give much better diff info
157 *
158 * @param string $text
159 * @return string
160 */
161 function segmentForDiff( $text ) {
162 return preg_replace( '/[\xc0-\xff][\x80-\xbf]*/', ' $0', $text );
163 }
164
165 /**
166 * @param string $text
167 * @return string
168 */
169 function unsegmentForDiff( $text ) {
170 return preg_replace( '/ ([\xc0-\xff][\x80-\xbf]*)/', '$1', $text );
171 }
172
173 /**
174 * auto convert to zh-hans and normalize special characters.
175 *
176 * @param string $string
177 * @param string $autoVariant Defaults to 'zh-hans'
178 * @return string
179 */
180 function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
181
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;
190
191 }
192
193 /**
194 * @param array $termsArray
195 * @return array
196 */
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;
203 }
204 }