0ba1ad91345aba7757789897c0be3c13cb807c40
[lhc/web/wiklou.git] / languages / classes / LanguageZh.php
1 <?php
2
3 $dir = dirname(__FILE__) . '/';
4 require_once( $dir . '../LanguageConverter.php' );
5 require_once( $dir . 'LanguageZh_hans.php' );
6
7 /**
8 * @ingroup Language
9 */
10 class ZhConverter extends LanguageConverter {
11
12 function __construct($langobj, $maincode,
13 $variants=array(),
14 $variantfallbacks=array(),
15 $markup=array(),
16 $flags = array(),
17 $manualLevel = array() ) {
18 $this->mDescCodeSep = ':';
19 $this->mDescVarSep = ';';
20 parent::__construct($langobj, $maincode,
21 $variants,
22 $variantfallbacks,
23 $markup,
24 $flags,
25 $manualLevel);
26 $names = array(
27 'zh' => '原文',
28 'zh-hans' => '简体',
29 'zh-hant' => '繁體',
30 'zh-cn' => '大陆',
31 'zh-tw' => '台灣',
32 'zh-hk' => '香港',
33 'zh-mo' => '澳門',
34 'zh-sg' => '新加坡',
35 'zh-my' => '大马',
36 );
37 $this->mVariantNames = array_merge($this->mVariantNames,$names);
38 }
39
40 function loadDefaultTables() {
41 require( dirname(__FILE__)."/../../includes/ZhConversion.php" );
42 $this->mTables = array(
43 'zh-hans' => new ReplacementArray( $zh2Hans ),
44 'zh-hant' => new ReplacementArray( $zh2Hant ),
45 'zh-cn' => new ReplacementArray( array_merge($zh2Hans, $zh2CN) ),
46 'zh-hk' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
47 'zh-mo' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
48 'zh-my' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
49 'zh-sg' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
50 'zh-tw' => new ReplacementArray( array_merge($zh2Hant, $zh2TW) ),
51 'zh' => new ReplacementArray
52 );
53 }
54
55 function postLoadTables() {
56 $this->mTables['zh-cn']->merge( $this->mTables['zh-hans'] );
57 $this->mTables['zh-hk']->merge( $this->mTables['zh-hant'] );
58 $this->mTables['zh-mo']->merge( $this->mTables['zh-hant'] );
59 $this->mTables['zh-my']->merge( $this->mTables['zh-hans'] );
60 $this->mTables['zh-sg']->merge( $this->mTables['zh-hans'] );
61 $this->mTables['zh-tw']->merge( $this->mTables['zh-hant'] );
62 }
63
64 /* there shouldn't be any latin text in Chinese conversion, so no need
65 to mark anything.
66 $noParse is there for compatibility with LanguageConvert::markNoConversion
67 */
68 function markNoConversion($text, $noParse = false) {
69 return $text;
70 }
71
72 function convertCategoryKey( $key ) {
73 return $this->autoConvert( $key, 'zh' );
74 }
75 }
76
77 /**
78 * class that handles both Traditional and Simplified Chinese
79 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
80 *
81 * @ingroup Language
82 */
83 class LanguageZh extends LanguageZh_hans {
84
85 function __construct() {
86 global $wgHooks;
87 parent::__construct();
88
89 $variants = array('zh','zh-hans','zh-hant','zh-cn','zh-hk','zh-mo','zh-my','zh-sg','zh-tw');
90 $variantfallbacks = array(
91 'zh' => array('zh-hans','zh-hant','zh-cn','zh-tw','zh-hk','zh-sg','zh-mo','zh-my'),
92 'zh-hans' => array('zh-cn','zh-sg','zh-my'),
93 'zh-hant' => array('zh-tw','zh-hk','zh-mo'),
94 'zh-cn' => array('zh-hans','zh-sg','zh-my'),
95 'zh-sg' => array('zh-hans','zh-cn','zh-my'),
96 'zh-my' => array('zh-hans','zh-sg','zh-cn'),
97 'zh-tw' => array('zh-hant','zh-hk','zh-mo'),
98 'zh-hk' => array('zh-hant','zh-mo','zh-tw'),
99 'zh-mo' => array('zh-hant','zh-hk','zh-tw'),
100 );
101 $ml=array(
102 'zh' => 'disable',
103 'zh-hans' => 'unidirectional',
104 'zh-hant' => 'unidirectional',
105 );
106
107 $this->mConverter = new ZhConverter( $this, 'zh',
108 $variants, $variantfallbacks,
109 array(),array(),
110 $ml);
111
112 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
113 }
114
115 # this should give much better diff info
116 function segmentForDiff( $text ) {
117 return preg_replace(
118 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
119 "' ' .\"$1\"", $text);
120 }
121
122 function unsegmentForDiff( $text ) {
123 return preg_replace(
124 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
125 "\"$1\"", $text);
126 }
127
128 // word segmentation
129 function stripForSearch( $string ) {
130 wfProfileIn( __METHOD__ );
131
132 // eventually this should be a word segmentation
133 // for now just treat each character as a word
134 // @fixme only do this for Han characters...
135 $t = preg_replace(
136 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
137 " $1", $string);
138
139 //always convert to zh-hans before indexing. it should be
140 //better to use zh-hans for search, since conversion from
141 //Traditional to Simplified is less ambiguous than the
142 //other way around
143
144 $t = $this->mConverter->autoConvert($t, 'zh-hans');
145 $t = parent::stripForSearch( $t );
146 wfProfileOut( __METHOD__ );
147 return $t;
148
149 }
150
151 function convertForSearchResult( $termsArray ) {
152 $terms = implode( '|', $termsArray );
153 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
154 $ret = array_unique( explode('|', $terms) );
155 return $ret;
156 }
157 }
158