242fea2f83e601cc9b0879649baab5d4927fd1d5
[lhc/web/wiklou.git] / languages / classes / LanguageZh.php
1 <?php
2 /**
3 * @addtogroup Language
4 */
5 require_once( dirname(__FILE__).'/../LanguageConverter.php' );
6 require_once( dirname(__FILE__).'/LanguageZh_hans.php' );
7
8 class ZhConverter extends LanguageConverter {
9 function loadDefaultTables() {
10 require( dirname(__FILE__)."/../../includes/ZhConversion.php" );
11 $this->mTables = array(
12 'zh-hans' => new ReplacementArray( $zh2Hans ),
13 'zh-hant' => new ReplacementArray( $zh2Hant ),
14 'zh-cn' => new ReplacementArray( array_merge($zh2Hans, $zh2CN) ),
15 'zh-tw' => new ReplacementArray( array_merge($zh2Hant, $zh2TW) ),
16 'zh-sg' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
17 'zh-hk' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
18 'zh' => new ReplacementArray
19 );
20 }
21
22 function postLoadTables() {
23 $this->mTables['zh-cn']->merge( $this->mTables['zh-hans'] );
24 $this->mTables['zh-tw']->merge( $this->mTables['zh-hant'] );
25 $this->mTables['zh-sg']->merge( $this->mTables['zh-hans'] );
26 $this->mTables['zh-hk']->merge( $this->mTables['zh-hant'] );
27 }
28
29 /* there shouldn't be any latin text in Chinese conversion, so no need
30 to mark anything.
31 $noParse is there for compatibility with LanguageConvert::markNoConversion
32 */
33 function markNoConversion($text, $noParse = false) {
34 return $text;
35 }
36
37 function convertCategoryKey( $key ) {
38 return $this->autoConvert( $key, 'zh' );
39 }
40 }
41
42 /* class that handles both Traditional and Simplified Chinese
43 right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
44 */
45 class LanguageZh extends LanguageZh_hans {
46
47 function __construct() {
48 global $wgHooks;
49 parent::__construct();
50
51 $variants = array('zh', 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-sg', 'zh-hk');
52 $variantfallbacks = array(
53 'zh' => 'zh-hans',
54 'zh-hans' => 'zh',
55 'zh-hant' => 'zh',
56 'zh-cn' => 'zh-hans',
57 'zh-sg' => 'zh-hans',
58 'zh-tw' => 'zh-hant',
59 'zh-hk' => 'zh-hant',
60 );
61
62 $this->mConverter = new ZhConverter( $this, 'zh', $variants, $variantfallbacks );
63
64 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
65 }
66
67 # this should give much better diff info
68 function segmentForDiff( $text ) {
69 return preg_replace(
70 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
71 "' ' .\"$1\"", $text);
72 }
73
74 function unsegmentForDiff( $text ) {
75 return preg_replace(
76 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
77 "\"$1\"", $text);
78 }
79
80 // word segmentation
81 function stripForSearch( $string ) {
82 $fname="LanguageZh::stripForSearch";
83 wfProfileIn( $fname );
84
85 // eventually this should be a word segmentation
86 // for now just treat each character as a word
87 $t = preg_replace(
88 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
89 "' ' .\"$1\"", $string);
90
91 //always convert to zh-hans before indexing. it should be
92 //better to use zh-hans for search, since conversion from
93 //Traditional to Simplified is less ambiguous than the
94 //other way around
95
96 $t = $this->mConverter->autoConvert($t, 'zh-hans');
97 $t = parent::stripForSearch( $t );
98 wfProfileOut( $fname );
99 return $t;
100
101 }
102
103 function convertForSearchResult( $termsArray ) {
104 $terms = implode( '|', $termsArray );
105 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
106 $ret = array_unique( explode('|', $terms) );
107 return $ret;
108 }
109 }