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