merged master
[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 /**
33 * @param $langobj Language
34 * @param $maincode string
35 * @param $variants array
36 * @param $variantfallbacks array
37 * @param $flags array
38 * @param $manualLevel array
39 */
40 function __construct( $langobj, $maincode,
41 $variants = array(),
42 $variantfallbacks = array(),
43 $flags = array(),
44 $manualLevel = array() ) {
45 $this->mDescCodeSep = ':';
46 $this->mDescVarSep = ';';
47 parent::__construct( $langobj, $maincode,
48 $variants,
49 $variantfallbacks,
50 $flags,
51 $manualLevel );
52 $names = array(
53 'zh' => '原文',
54 'zh-hans' => '简体',
55 'zh-hant' => '繁體',
56 'zh-cn' => '大陆',
57 'zh-tw' => '台灣',
58 'zh-hk' => '香港',
59 'zh-mo' => '澳門',
60 'zh-sg' => '新加坡',
61 'zh-my' => '大马',
62 );
63 $this->mVariantNames = array_merge( $this->mVariantNames, $names );
64 }
65
66 function loadDefaultTables() {
67 require( __DIR__ . "/../../includes/ZhConversion.php" );
68 $this->mTables = array(
69 'zh-hans' => new ReplacementArray( $zh2Hans ),
70 'zh-hant' => new ReplacementArray( $zh2Hant ),
71 'zh-cn' => new ReplacementArray( array_merge( $zh2Hans, $zh2CN ) ),
72 'zh-hk' => new ReplacementArray( array_merge( $zh2Hant, $zh2HK ) ),
73 'zh-mo' => new ReplacementArray( array_merge( $zh2Hant, $zh2HK ) ),
74 'zh-my' => new ReplacementArray( array_merge( $zh2Hans, $zh2SG ) ),
75 'zh-sg' => new ReplacementArray( array_merge( $zh2Hans, $zh2SG ) ),
76 'zh-tw' => new ReplacementArray( array_merge( $zh2Hant, $zh2TW ) ),
77 'zh' => new ReplacementArray
78 );
79 }
80
81 function postLoadTables() {
82 $this->mTables['zh-cn']->merge( $this->mTables['zh-hans'] );
83 $this->mTables['zh-hk']->merge( $this->mTables['zh-hant'] );
84 $this->mTables['zh-mo']->merge( $this->mTables['zh-hant'] );
85 $this->mTables['zh-my']->merge( $this->mTables['zh-hans'] );
86 $this->mTables['zh-sg']->merge( $this->mTables['zh-hans'] );
87 $this->mTables['zh-tw']->merge( $this->mTables['zh-hant'] );
88 }
89
90 /**
91 * there shouldn't be any latin text in Chinese conversion, so no need
92 * to mark anything.
93 * $noParse is there for compatibility with LanguageConvert::markNoConversion
94 *
95 * @param $text string
96 * @param $noParse bool
97 *
98 * @return string
99 */
100 function markNoConversion( $text, $noParse = false ) {
101 return $text;
102 }
103
104 /**
105 * @param $key string
106 * @return String
107 */
108 function convertCategoryKey( $key ) {
109 return $this->autoConvert( $key, 'zh' );
110 }
111 }
112
113 /**
114 * class that handles both Traditional and Simplified Chinese
115 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
116 *
117 * @ingroup Language
118 */
119 class LanguageZh extends LanguageZh_hans {
120
121 function __construct() {
122 global $wgHooks;
123 parent::__construct();
124
125 $variants = array( 'zh', 'zh-hans', 'zh-hant', 'zh-cn', 'zh-hk', 'zh-mo', 'zh-my', 'zh-sg', 'zh-tw' );
126
127 $variantfallbacks = array(
128 'zh' => array( 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ),
129 'zh-hans' => array( 'zh-cn', 'zh-sg', 'zh-my' ),
130 'zh-hant' => array( 'zh-tw', 'zh-hk', 'zh-mo' ),
131 'zh-cn' => array( 'zh-hans', 'zh-sg', 'zh-my' ),
132 'zh-sg' => array( 'zh-hans', 'zh-cn', 'zh-my' ),
133 'zh-my' => array( 'zh-hans', 'zh-sg', 'zh-cn' ),
134 'zh-tw' => array( 'zh-hant', 'zh-hk', 'zh-mo' ),
135 'zh-hk' => array( 'zh-hant', 'zh-mo', 'zh-tw' ),
136 'zh-mo' => array( 'zh-hant', 'zh-hk', 'zh-tw' ),
137 );
138 $ml = array(
139 'zh' => 'disable',
140 'zh-hans' => 'unidirectional',
141 'zh-hant' => 'unidirectional',
142 );
143
144 $this->mConverter = new ZhConverter( $this, 'zh',
145 $variants, $variantfallbacks,
146 array(),
147 $ml );
148
149 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
150 }
151
152 /**
153 * this should give much better diff info
154 *
155 * @param $text string
156 * @return string
157 */
158 function segmentForDiff( $text ) {
159 return preg_replace(
160 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
161 "' ' .\"$1\"", $text );
162 }
163
164 /**
165 * @param $text string
166 * @return string
167 */
168 function unsegmentForDiff( $text ) {
169 return preg_replace(
170 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
171 "\"$1\"", $text );
172 }
173
174 /**
175 * auto convert to zh-hans and normalize special characters.
176 *
177 * @param $string String
178 * @param $autoVariant String, default to 'zh-hans'
179 * @return String
180 */
181 function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
182 wfProfileIn( __METHOD__ );
183
184 // always convert to zh-hans before indexing. it should be
185 // better to use zh-hans for search, since conversion from
186 // Traditional to Simplified is less ambiguous than the
187 // other way around
188 $s = $this->mConverter->autoConvert( $string, $autoVariant );
189 // LanguageZh_hans::normalizeForSearch
190 $s = parent::normalizeForSearch( $s );
191 wfProfileOut( __METHOD__ );
192 return $s;
193
194 }
195
196 /**
197 * @param $termsArray array
198 * @return array
199 */
200 function convertForSearchResult( $termsArray ) {
201 $terms = implode( '|', $termsArray );
202 $terms = self::convertDoubleWidth( $terms );
203 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
204 $ret = array_unique( explode( '|', $terms ) );
205 return $ret;
206 }
207 }
208