]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - languages/classes/LanguageZh.php
MediaWiki 1.15.4-scripts
[autoinstalls/mediawiki.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                 $variantfallbacks = array(
137                         'zh'      => array('zh-hans','zh-hant','zh-cn','zh-tw','zh-hk','zh-sg','zh-mo','zh-my'),
138                         'zh-hans' => array('zh-cn','zh-sg','zh-my'),
139                         'zh-hant' => array('zh-tw','zh-hk','zh-mo'),
140                         'zh-cn'   => array('zh-hans','zh-sg','zh-my'),
141                         'zh-sg'   => array('zh-hans','zh-cn','zh-my'),
142                         'zh-my'   => array('zh-hans','zh-sg','zh-cn'),
143                         'zh-tw'   => array('zh-hant','zh-hk','zh-mo'),
144                         'zh-hk'   => array('zh-hant','zh-mo','zh-tw'),
145                         'zh-mo'   => array('zh-hant','zh-hk','zh-tw'),
146                 );
147                 $ml=array(
148                         'zh'      => 'disable',
149                         'zh-hans' => 'unidirectional',
150                         'zh-hant' => 'unidirectional',
151                 );
152
153                 $this->mConverter = new ZhConverter( $this, 'zh',
154                                                                 $variants, $variantfallbacks,
155                                                                 array(),array(),
156                                                                 $ml);
157
158                 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
159         }
160
161         # this should give much better diff info
162         function segmentForDiff( $text ) {
163                 return preg_replace(
164                         "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
165                         "' ' .\"$1\"", $text);
166         }
167
168         function unsegmentForDiff( $text ) {
169                 return preg_replace(
170                         "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
171                         "\"$1\"", $text);
172         }
173
174         // word segmentation
175         function stripForSearch( $string ) {
176                 wfProfileIn( __METHOD__ );
177
178                 // eventually this should be a word segmentation
179                 // for now just treat each character as a word
180                 // @fixme only do this for Han characters...
181                 $t = preg_replace(
182                                 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
183                                 " $1", $string);
184
185         //always convert to zh-hans before indexing. it should be
186                 //better to use zh-hans for search, since conversion from
187                 //Traditional to Simplified is less ambiguous than the
188                 //other way around
189
190                 $t = $this->mConverter->autoConvert($t, 'zh-hans');
191                 $t = parent::stripForSearch( $t );
192                 wfProfileOut( __METHOD__ );
193                 return $t;
194
195         }
196
197         function convertForSearchResult( $termsArray ) {
198                 $terms = implode( '|', $termsArray );
199                 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
200                 $ret = array_unique( explode('|', $terms) );
201                 return $ret;
202         }
203 }
204