Remove require_once for language classes
[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 /**
25 * @ingroup Language
26 */
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
35 */
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' => '大马',
58 );
59 $this->mVariantNames = array_merge( $this->mVariantNames, $names );
60 }
61
62 function loadDefaultTables() {
63 require __DIR__ . "/../../includes/ZhConversion.php";
64 $this->mTables = array(
65 'zh-hans' => new ReplacementArray( $zh2Hans ),
66 'zh-hant' => new ReplacementArray( $zh2Hant ),
67 'zh-cn' => new ReplacementArray( $zh2CN ),
68 'zh-hk' => new ReplacementArray( $zh2HK ),
69 'zh-mo' => new ReplacementArray( $zh2HK ),
70 'zh-my' => new ReplacementArray( $zh2CN ),
71 'zh-sg' => new ReplacementArray( $zh2CN ),
72 'zh-tw' => new ReplacementArray( $zh2TW ),
73 'zh' => new ReplacementArray
74 );
75 }
76
77 function postLoadTables() {
78 $this->mTables['zh-cn']->setArray(
79 $this->mTables['zh-cn']->getArray() + $this->mTables['zh-hans']->getArray()
80 );
81 $this->mTables['zh-hk']->setArray(
82 $this->mTables['zh-hk']->getArray() + $this->mTables['zh-hant']->getArray()
83 );
84 $this->mTables['zh-mo']->setArray(
85 $this->mTables['zh-mo']->getArray() + $this->mTables['zh-hant']->getArray()
86 );
87 $this->mTables['zh-my']->setArray(
88 $this->mTables['zh-my']->getArray() + $this->mTables['zh-hans']->getArray()
89 );
90 $this->mTables['zh-sg']->setArray(
91 $this->mTables['zh-sg']->getArray() + $this->mTables['zh-hans']->getArray()
92 );
93 $this->mTables['zh-tw']->setArray(
94 $this->mTables['zh-tw']->getArray() + $this->mTables['zh-hant']->getArray()
95 );
96 }
97
98 /**
99 * @param string $key
100 * @return string
101 */
102 function convertCategoryKey( $key ) {
103 return $this->autoConvert( $key, 'zh' );
104 }
105 }
106
107 /**
108 * class that handles both Traditional and Simplified Chinese
109 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
110 *
111 * @ingroup Language
112 */
113 class LanguageZh extends LanguageZh_hans {
114 function __construct() {
115 global $wgHooks;
116
117 parent::__construct();
118
119 $variants = array(
120 'zh',
121 'zh-hans',
122 'zh-hant',
123 'zh-cn',
124 'zh-hk',
125 'zh-mo',
126 'zh-my',
127 'zh-sg',
128 'zh-tw'
129 );
130
131 $variantfallbacks = array(
132 'zh' => array( 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ),
133 'zh-hans' => array( 'zh-cn', 'zh-sg', 'zh-my' ),
134 'zh-hant' => array( 'zh-tw', 'zh-hk', 'zh-mo' ),
135 'zh-cn' => array( 'zh-hans', 'zh-sg', 'zh-my' ),
136 'zh-sg' => array( 'zh-hans', 'zh-cn', 'zh-my' ),
137 'zh-my' => array( 'zh-hans', 'zh-sg', 'zh-cn' ),
138 'zh-tw' => array( 'zh-hant', 'zh-hk', 'zh-mo' ),
139 'zh-hk' => array( 'zh-hant', 'zh-mo', 'zh-tw' ),
140 'zh-mo' => array( 'zh-hant', 'zh-hk', 'zh-tw' ),
141 );
142 $ml = array(
143 'zh' => 'disable',
144 'zh-hans' => 'unidirectional',
145 'zh-hant' => 'unidirectional',
146 );
147
148 $this->mConverter = new ZhConverter( $this, 'zh',
149 $variants, $variantfallbacks,
150 array(),
151 $ml );
152
153 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
154 }
155
156 /**
157 * this should give much better diff info
158 *
159 * @param string $text
160 * @return string
161 */
162 function segmentForDiff( $text ) {
163 return preg_replace( '/[\xc0-\xff][\x80-\xbf]*/', ' $0', $text );
164 }
165
166 /**
167 * @param string $text
168 * @return string
169 */
170 function unsegmentForDiff( $text ) {
171 return preg_replace( '/ ([\xc0-\xff][\x80-\xbf]*)/', '$1', $text );
172 }
173
174 /**
175 * auto convert to zh-hans and normalize special characters.
176 *
177 * @param string $string
178 * @param string $autoVariant Defaults to 'zh-hans'
179 * @return string
180 */
181 function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
182
183 // always convert to zh-hans before indexing. it should be
184 // better to use zh-hans for search, since conversion from
185 // Traditional to Simplified is less ambiguous than the
186 // other way around
187 $s = $this->mConverter->autoConvert( $string, $autoVariant );
188 // LanguageZh_hans::normalizeForSearch
189 $s = parent::normalizeForSearch( $s );
190 return $s;
191
192 }
193
194 /**
195 * @param array $termsArray
196 * @return array
197 */
198 function convertForSearchResult( $termsArray ) {
199 $terms = implode( '|', $termsArray );
200 $terms = self::convertDoubleWidth( $terms );
201 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
202 $ret = array_unique( explode( '|', $terms ) );
203 return $ret;
204 }
205 }