Merge "Add pluggable talk page poster and use it for mediawiki.feedback"
[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 global $wgHooks;
119
120 parent::__construct();
121
122 $variants = array(
123 'zh',
124 'zh-hans',
125 'zh-hant',
126 'zh-cn',
127 'zh-hk',
128 'zh-mo',
129 'zh-my',
130 'zh-sg',
131 'zh-tw'
132 );
133
134 $variantfallbacks = array(
135 'zh' => array( 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ),
136 'zh-hans' => array( 'zh-cn', 'zh-sg', 'zh-my' ),
137 'zh-hant' => array( 'zh-tw', 'zh-hk', 'zh-mo' ),
138 'zh-cn' => array( 'zh-hans', 'zh-sg', 'zh-my' ),
139 'zh-sg' => array( 'zh-hans', 'zh-cn', 'zh-my' ),
140 'zh-my' => array( 'zh-hans', 'zh-sg', 'zh-cn' ),
141 'zh-tw' => array( 'zh-hant', 'zh-hk', 'zh-mo' ),
142 'zh-hk' => array( 'zh-hant', 'zh-mo', 'zh-tw' ),
143 'zh-mo' => array( 'zh-hant', 'zh-hk', 'zh-tw' ),
144 );
145 $ml = array(
146 'zh' => 'disable',
147 'zh-hans' => 'unidirectional',
148 'zh-hant' => 'unidirectional',
149 );
150
151 $this->mConverter = new ZhConverter( $this, 'zh',
152 $variants, $variantfallbacks,
153 array(),
154 $ml );
155
156 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
157 }
158
159 /**
160 * this should give much better diff info
161 *
162 * @param string $text
163 * @return string
164 */
165 function segmentForDiff( $text ) {
166 return preg_replace( '/[\xc0-\xff][\x80-\xbf]*/', ' $0', $text );
167 }
168
169 /**
170 * @param string $text
171 * @return string
172 */
173 function unsegmentForDiff( $text ) {
174 return preg_replace( '/ ([\xc0-\xff][\x80-\xbf]*)/', '$1', $text );
175 }
176
177 /**
178 * auto convert to zh-hans and normalize special characters.
179 *
180 * @param string $string
181 * @param string $autoVariant Defaults to 'zh-hans'
182 * @return string
183 */
184 function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
185
186 // always convert to zh-hans before indexing. it should be
187 // better to use zh-hans for search, since conversion from
188 // Traditional to Simplified is less ambiguous than the
189 // other way around
190 $s = $this->mConverter->autoConvert( $string, $autoVariant );
191 // LanguageZh_hans::normalizeForSearch
192 $s = parent::normalizeForSearch( $s );
193 return $s;
194
195 }
196
197 /**
198 * @param array $termsArray
199 * @return array
200 */
201 function convertForSearchResult( $termsArray ) {
202 $terms = implode( '|', $termsArray );
203 $terms = self::convertDoubleWidth( $terms );
204 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
205 $ret = array_unique( explode( '|', $terms ) );
206 return $ret;
207 }
208 }