3 * Chinese specific code.
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.
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.
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
27 class ZhConverter
extends LanguageConverter
{
29 * @param Language $langobj
30 * @param string $maincode
31 * @param array $variants
32 * @param array $variantfallbacks
34 * @param array $manualLevel
36 function __construct( Language
$langobj, $maincode,
38 $variantfallbacks = [],
42 $this->mDescCodeSep
= ':';
43 $this->mDescVarSep
= ';';
44 parent
::__construct( $langobj, $maincode,
61 $this->mVariantNames
= array_merge( $this->mVariantNames
, $names );
64 function loadDefaultTables() {
66 'zh-hans' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2Hans ),
67 'zh-hant' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2Hant ),
68 'zh-cn' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2CN ),
69 'zh-hk' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2HK ),
70 'zh-mo' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2HK ),
71 'zh-my' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2CN ),
72 'zh-sg' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2CN ),
73 'zh-tw' => new ReplacementArray( MediaWiki\Languages\Data\ZhConversion
::$zh2TW ),
74 'zh' => new ReplacementArray
78 function postLoadTables() {
79 $this->mTables
['zh-cn']->setArray(
80 $this->mTables
['zh-cn']->getArray() +
$this->mTables
['zh-hans']->getArray()
82 $this->mTables
['zh-hk']->setArray(
83 $this->mTables
['zh-hk']->getArray() +
$this->mTables
['zh-hant']->getArray()
85 $this->mTables
['zh-mo']->setArray(
86 $this->mTables
['zh-mo']->getArray() +
$this->mTables
['zh-hant']->getArray()
88 $this->mTables
['zh-my']->setArray(
89 $this->mTables
['zh-my']->getArray() +
$this->mTables
['zh-hans']->getArray()
91 $this->mTables
['zh-sg']->setArray(
92 $this->mTables
['zh-sg']->getArray() +
$this->mTables
['zh-hans']->getArray()
94 $this->mTables
['zh-tw']->setArray(
95 $this->mTables
['zh-tw']->getArray() +
$this->mTables
['zh-hant']->getArray()
103 function convertCategoryKey( $key ) {
104 return $this->autoConvert( $key, 'zh' );
109 * class that handles both Traditional and Simplified Chinese
110 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
114 class LanguageZh
extends LanguageZh_hans
{
115 function __construct() {
116 parent
::__construct();
130 $variantfallbacks = [
131 'zh' => [ 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ],
132 'zh-hans' => [ 'zh-cn', 'zh-sg', 'zh-my' ],
133 'zh-hant' => [ 'zh-tw', 'zh-hk', 'zh-mo' ],
134 'zh-cn' => [ 'zh-hans', 'zh-sg', 'zh-my' ],
135 'zh-sg' => [ 'zh-hans', 'zh-cn', 'zh-my' ],
136 'zh-my' => [ 'zh-hans', 'zh-sg', 'zh-cn' ],
137 'zh-tw' => [ 'zh-hant', 'zh-hk', 'zh-mo' ],
138 'zh-hk' => [ 'zh-hant', 'zh-mo', 'zh-tw' ],
139 'zh-mo' => [ 'zh-hant', 'zh-hk', 'zh-tw' ],
143 'zh-hans' => 'unidirectional',
144 'zh-hant' => 'unidirectional',
147 $this->mConverter
= new ZhConverter( $this, 'zh',
148 $variants, $variantfallbacks,
155 * this should give much better diff info
157 * @param string $text
160 function segmentForDiff( $text ) {
161 return preg_replace( '/[\xc0-\xff][\x80-\xbf]*/', ' $0', $text );
165 * @param string $text
168 function unsegmentForDiff( $text ) {
169 return preg_replace( '/ ([\xc0-\xff][\x80-\xbf]*)/', '$1', $text );
173 * auto convert to zh-hans and normalize special characters.
175 * @param string $string
176 * @param string $autoVariant Defaults to 'zh-hans'
179 function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
180 // always convert to zh-hans before indexing. it should be
181 // better to use zh-hans for search, since conversion from
182 // Traditional to Simplified is less ambiguous than the
184 $s = $this->mConverter
->autoConvert( $string, $autoVariant );
185 // LanguageZh_hans::normalizeForSearch
186 $s = parent
::normalizeForSearch( $s );
191 * @param array $termsArray
194 function convertForSearchResult( $termsArray ) {
195 $terms = implode( '|', $termsArray );
196 $terms = self
::convertDoubleWidth( $terms );
197 $terms = implode( '|', $this->mConverter
->autoConvertToAllVariants( $terms ) );
198 $ret = array_unique( explode( '|', $terms ) );