6a004a080d552ffdeea068bb7c6227f6caca0013
[lhc/web/wiklou.git] / languages / classes / LanguageZh.php
1 <?php
2
3 require_once( dirname(__FILE__).'/../LanguageConverter.php' );
4 require_once( dirname(__FILE__).'/LanguageZh_hans.php' );
5
6 /**
7 * @ingroup Language
8 */
9 class ZhConverter extends LanguageConverter {
10
11 function __construct($langobj, $maincode,
12 $variants=array(),
13 $variantfallbacks=array(),
14 $flags = array(),
15 $manualLevel = array() ) {
16 $this->mDescCodeSep = ':';
17 $this->mDescVarSep = ';';
18 parent::__construct($langobj, $maincode,
19 $variants,
20 $variantfallbacks,
21 $flags,
22 $manualLevel);
23 $names = array(
24 'zh' => '原文',
25 'zh-hans' => '简体',
26 'zh-hant' => '繁體',
27 'zh-cn' => '大陆',
28 'zh-tw' => '台灣',
29 'zh-hk' => '香港',
30 'zh-mo' => '澳門',
31 'zh-sg' => '新加坡',
32 'zh-my' => '大马',
33 );
34 $this->mVariantNames = array_merge($this->mVariantNames,$names);
35 $this->loadNamespaceTables();
36 }
37
38 function loadNamespaceTables() {
39 global $wgMetaNamespace;
40 $nsproject = $wgMetaNamespace;
41 $projecttable = array(
42 'Wikipedia' => '维基百科',
43 'Wikisource' => '维基文库',
44 'Wikinews' => '维基新闻',
45 'Wiktionary' => '维基词典',
46 'Wikibooks' => '维基教科书',
47 'Wikiquote' => '维基语录',
48 );
49 $this->mNamespaceTables['zh-hans'] = array(
50 'Media' => '媒体',
51 'Special' => '特殊',
52 'Talk' => '讨论',
53 'User' => '用户',
54 'User talk' => '用户讨论',
55 $nsproject
56 => isset($projecttable[$nsproject]) ?
57 $projecttable[$nsproject] : $nsproject,
58 $nsproject . ' talk'
59 => isset($projecttable[$nsproject]) ?
60 $projecttable[$nsproject] . '讨论' : $nsproject . '讨论',
61 'File' => '文件',
62 'File talk' => '文件讨论',
63 'MediaWiki' => 'MediaWiki',
64 'MediaWiki talk' => 'MediaWiki讨论',
65 'Template' => '模板',
66 'Template talk' => '模板讨论',
67 'Help' => '帮助',
68 'Help talk' => '帮助讨论',
69 'Category' => '分类',
70 'Category talk' => '分类讨论',
71 );
72 $this->mNamespaceTables['zh-hant'] = array_merge($this->mNamespaceTables['zh-hans']);
73 $this->mNamespaceTables['zh-hant']['File'] = '檔案';
74 $this->mNamespaceTables['zh-hant']['File talk'] = '檔案討論';
75 $this->mNamespaceTables['zh'] = array_merge($this->mNamespaceTables['zh-hans']);
76 $this->mNamespaceTables['zh-cn'] = array_merge($this->mNamespaceTables['zh-hans']);
77 $this->mNamespaceTables['zh-hk'] = array_merge($this->mNamespaceTables['zh-hant']);
78 $this->mNamespaceTables['zh-mo'] = array_merge($this->mNamespaceTables['zh-hant']);
79 $this->mNamespaceTables['zh-my'] = array_merge($this->mNamespaceTables['zh-hans']);
80 $this->mNamespaceTables['zh-sg'] = array_merge($this->mNamespaceTables['zh-hans']);
81 $this->mNamespaceTables['zh-tw'] = array_merge($this->mNamespaceTables['zh-hant']);
82 }
83
84 function loadDefaultTables() {
85 require( dirname(__FILE__)."/../../includes/ZhConversion.php" );
86 $this->mTables = array(
87 'zh-hans' => new ReplacementArray( $zh2Hans ),
88 'zh-hant' => new ReplacementArray( $zh2Hant ),
89 'zh-cn' => new ReplacementArray( array_merge($zh2Hans, $zh2CN) ),
90 'zh-hk' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
91 'zh-mo' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
92 'zh-my' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
93 'zh-sg' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
94 'zh-tw' => new ReplacementArray( array_merge($zh2Hant, $zh2TW) ),
95 'zh' => new ReplacementArray
96 );
97 }
98
99 function postLoadTables() {
100 $this->mTables['zh-cn']->merge( $this->mTables['zh-hans'] );
101 $this->mTables['zh-hk']->merge( $this->mTables['zh-hant'] );
102 $this->mTables['zh-mo']->merge( $this->mTables['zh-hant'] );
103 $this->mTables['zh-my']->merge( $this->mTables['zh-hans'] );
104 $this->mTables['zh-sg']->merge( $this->mTables['zh-hans'] );
105 $this->mTables['zh-tw']->merge( $this->mTables['zh-hant'] );
106 }
107
108 /* there shouldn't be any latin text in Chinese conversion, so no need
109 to mark anything.
110 $noParse is there for compatibility with LanguageConvert::markNoConversion
111 */
112 function markNoConversion($text, $noParse = false) {
113 return $text;
114 }
115
116 function convertCategoryKey( $key ) {
117 return $this->autoConvert( $key, 'zh' );
118 }
119 }
120
121 /**
122 * class that handles both Traditional and Simplified Chinese
123 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
124 *
125 * @ingroup Language
126 */
127 class LanguageZh extends LanguageZh_hans {
128
129 function __construct() {
130 global $wgHooks;
131 parent::__construct();
132
133 $variants = array('zh','zh-hans','zh-hant','zh-cn','zh-hk','zh-mo','zh-my','zh-sg','zh-tw');
134
135 $variantfallbacks = array(
136 'zh' => array('zh-hans','zh-hant','zh-cn','zh-tw','zh-hk','zh-sg','zh-mo','zh-my'),
137 'zh-hans' => array('zh-cn','zh-sg','zh-my'),
138 'zh-hant' => array('zh-tw','zh-hk','zh-mo'),
139 'zh-cn' => array('zh-hans','zh-sg','zh-my'),
140 'zh-sg' => array('zh-hans','zh-cn','zh-my'),
141 'zh-my' => array('zh-hans','zh-sg','zh-cn'),
142 'zh-tw' => array('zh-hant','zh-hk','zh-mo'),
143 'zh-hk' => array('zh-hant','zh-mo','zh-tw'),
144 'zh-mo' => array('zh-hant','zh-hk','zh-tw'),
145 );
146 $ml=array(
147 'zh' => 'disable',
148 'zh-hans' => 'unidirectional',
149 'zh-hant' => 'unidirectional',
150 );
151
152 $this->mConverter = new ZhConverter( $this, 'zh',
153 $variants, $variantfallbacks,
154 array(),
155 $ml);
156
157 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
158 }
159
160 # this should give much better diff info
161 function segmentForDiff( $text ) {
162 return preg_replace(
163 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
164 "' ' .\"$1\"", $text);
165 }
166
167 function unsegmentForDiff( $text ) {
168 return preg_replace(
169 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
170 "\"$1\"", $text);
171 }
172
173 // word segmentation
174 function stripForSearch( $string, $doStrip = true, $autoVariant = 'zh-hans' ) {
175 wfProfileIn( __METHOD__ );
176
177 // always convert to zh-hans before indexing. it should be
178 // better to use zh-hans for search, since conversion from
179 // Traditional to Simplified is less ambiguous than the
180 // other way around
181 $s = $this->mConverter->autoConvert( $string, $autoVariant );
182 // LanguageZh_hans::stripForSearch
183 $s = parent::stripForSearch( $s, $doStrip );
184 wfProfileOut( __METHOD__ );
185 return $s;
186
187 }
188
189 function convertForSearchResult( $termsArray ) {
190 $terms = implode( '|', $termsArray );
191 $terms = self::convertDoubleWidth( $terms );
192 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
193 $ret = array_unique( explode('|', $terms) );
194 return $ret;
195 }
196 }
197