Code refactoring for the language conversion system:
[lhc/web/wiklou.git] / languages / LanguageZh.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Language
5 */
6 require_once( "LanguageConverter.php" );
7 require_once( "LanguageZh_cn.php");
8 require_once( "LanguageZh_tw.php");
9 require_once( "LanguageZh_sg.php");
10 require_once( "LanguageZh_hk.php");
11
12 class ZhConverter extends LanguageConverter {
13 function loadDefaultTables() {
14 require( "includes/ZhConversion.php" );
15 $this->mTables = array();
16 $this->mTables['zh-cn'] = $zh2CN;
17 $this->mTables['zh-tw'] = $zh2TW;
18 $this->mTables['zh-sg'] = array_merge($zh2CN, $zh2SG);
19 $this->mTables['zh-hk'] = array_merge($zh2TW, $zh2HK);
20 $this->mTables['zh'] = array();
21 }
22
23 function postLoadTables() {
24 $this->mTables['zh-sg'] = array_merge($this->mTables['zh-cn'], $this->mTables['zh-sg']);
25 $this->mTables['zh-hk'] = array_merge($this->mTables['zh-tw'], $this->mTables['zh-hk']);
26 }
27 }
28
29
30 /* class that handles both Traditional and Simplified Chinese
31 right now it only distinguish zh_cn, zh_tw, zh_sg and zh_hk.
32 */
33 class LanguageZh extends LanguageZh_cn {
34
35 function LanguageZh() {
36 global $wgHooks;
37 $this->mConverter = new ZhConverter($this, 'zh',
38 array('zh', 'zh-cn', 'zh-tw', 'zh-sg', 'zh-hk'),
39 array('zh'=>'zh-cn',
40 'zh-cn'=>'zh-sg',
41 'zh-sg'=>'zh-cn',
42 'zh-tw'=>'zh-hk',
43 'zh-hk'=>'zh-tw'));
44 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
45 }
46
47
48 # this should give much better diff info
49 function segmentForDiff( $text ) {
50 return preg_replace(
51 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
52 "' ' .\"$1\"", $text);
53 }
54
55 function unsegmentForDiff( $text ) {
56 return preg_replace(
57 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
58 "\"$1\"", $text);
59 }
60
61 // word segmentation
62 function stripForSearch( $string ) {
63 $fname="LanguageZh::stripForSearch";
64 wfProfileIn( $fname );
65
66 // eventually this should be a word segmentation
67 // for now just treat each character as a word
68 $t = preg_replace(
69 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
70 "' ' .\"$1\"", $string);
71
72 //always convert to zh-cn before indexing. it should be
73 //better to use zh-cn for search, since conversion from
74 //Traditional to Simplified is less ambiguous than the
75 //other way around
76
77 $t = $this->mConverter->autoConvert($t, 'zh-cn');
78 $t = LanguageUtf8::stripForSearch( $t );
79 wfProfileOut( $fname );
80 return $t;
81
82 }
83
84 function convertForSearchResult( $termsArray ) {
85 $terms = implode( '|', $termsArray );
86 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
87 $ret = array_unique( explode('|', $terms) );
88 return $ret;
89 }
90
91 }
92 ?>