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