]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - languages/classes/LanguageZh.php
MediaWiki 1.16.0
[autoinstallsdev/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                                                                 $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         /**
174          * auto convert to zh-hans and normalize special characters.
175          *
176          * @param $string String
177          * @param $autoVariant String, default to 'zh-hans'
178          * @return String
179          */
180         function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
181                 wfProfileIn( __METHOD__ );
182
183                 // always convert to zh-hans before indexing. it should be
184                 // better to use zh-hans for search, since conversion from
185                 // Traditional to Simplified is less ambiguous than the
186                 // other way around
187                 $s = $this->mConverter->autoConvert( $string, $autoVariant );
188                 // LanguageZh_hans::normalizeForSearch
189                 $s = parent::normalizeForSearch( $s );
190                 wfProfileOut( __METHOD__ );
191                 return $s;
192
193         }
194
195         function convertForSearchResult( $termsArray ) {
196                 $terms = implode( '|', $termsArray );
197                 $terms = self::convertDoubleWidth( $terms );
198                 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
199                 $ret = array_unique( explode('|', $terms) );
200                 return $ret;
201         }
202 }
203