* (bug 14604) Update LanguageConverter for compatibility on -{*|xxx}- usage
[lhc/web/wiklou.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 }
38
39 function loadDefaultTables() {
40 require( dirname(__FILE__)."/../../includes/ZhConversion.php" );
41 $this->mTables = array(
42 'zh-hans' => new ReplacementArray( $zh2Hans ),
43 'zh-hant' => new ReplacementArray( $zh2Hant ),
44 'zh-cn' => new ReplacementArray( array_merge($zh2Hans, $zh2CN) ),
45 'zh-hk' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
46 'zh-mo' => new ReplacementArray( array_merge($zh2Hant, $zh2HK) ),
47 'zh-my' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
48 'zh-sg' => new ReplacementArray( array_merge($zh2Hans, $zh2SG) ),
49 'zh-tw' => new ReplacementArray( array_merge($zh2Hant, $zh2TW) ),
50 'zh' => new ReplacementArray
51 );
52 }
53
54 function postLoadTables() {
55 $this->mTables['zh-cn']->merge( $this->mTables['zh-hans'] );
56 $this->mTables['zh-hk']->merge( $this->mTables['zh-hant'] );
57 $this->mTables['zh-mo']->merge( $this->mTables['zh-hant'] );
58 $this->mTables['zh-my']->merge( $this->mTables['zh-hans'] );
59 $this->mTables['zh-sg']->merge( $this->mTables['zh-hans'] );
60 $this->mTables['zh-tw']->merge( $this->mTables['zh-hant'] );
61 }
62
63 /* there shouldn't be any latin text in Chinese conversion, so no need
64 to mark anything.
65 $noParse is there for compatibility with LanguageConvert::markNoConversion
66 */
67 function markNoConversion($text, $noParse = false) {
68 return $text;
69 }
70
71 function convertCategoryKey( $key ) {
72 return $this->autoConvert( $key, 'zh' );
73 }
74 }
75
76 /**
77 * class that handles both Traditional and Simplified Chinese
78 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
79 *
80 * @ingroup Language
81 */
82 class LanguageZh extends LanguageZh_hans {
83
84 function __construct() {
85 global $wgHooks;
86 parent::__construct();
87
88 $variants = array('zh','zh-hans','zh-hant','zh-cn','zh-hk','zh-mo','zh-my','zh-sg','zh-tw');
89 $variantfallbacks = array(
90 'zh' => array('zh-hans','zh-hant','zh-cn','zh-tw','zh-hk','zh-sg','zh-mo','zh-my'),
91 'zh-hans' => array('zh-cn','zh-sg','zh-my'),
92 'zh-hant' => array('zh-tw','zh-hk','zh-mo'),
93 'zh-cn' => array('zh-hans','zh-sg','zh-my'),
94 'zh-sg' => array('zh-hans','zh-cn','zh-my'),
95 'zh-my' => array('zh-hans','zh-sg','zh-cn'),
96 'zh-tw' => array('zh-hant','zh-hk','zh-mo'),
97 'zh-hk' => array('zh-hant','zh-mo','zh-tw'),
98 'zh-mo' => array('zh-hant','zh-hk','zh-tw'),
99 );
100 $ml=array(
101 'zh' => 'disable',
102 'zh-hans' => 'unidirectional',
103 'zh-hant' => 'unidirectional',
104 );
105
106 $this->mConverter = new ZhConverter( $this, 'zh',
107 $variants, $variantfallbacks,
108 array(),array(),
109 $ml);
110
111 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
112 }
113
114 # this should give much better diff info
115 function segmentForDiff( $text ) {
116 return preg_replace(
117 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
118 "' ' .\"$1\"", $text);
119 }
120
121 function unsegmentForDiff( $text ) {
122 return preg_replace(
123 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
124 "\"$1\"", $text);
125 }
126
127 // word segmentation
128 function stripForSearch( $string ) {
129 $fname="LanguageZh::stripForSearch";
130 wfProfileIn( $fname );
131
132 // eventually this should be a word segmentation
133 // for now just treat each character as a word
134 $t = preg_replace(
135 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
136 "' ' .\"$1\"", $string);
137
138 //always convert to zh-hans before indexing. it should be
139 //better to use zh-hans for search, since conversion from
140 //Traditional to Simplified is less ambiguous than the
141 //other way around
142
143 $t = $this->mConverter->autoConvert($t, 'zh-hans');
144 $t = parent::stripForSearch( $t );
145 wfProfileOut( $fname );
146 return $t;
147
148 }
149
150 function convertForSearchResult( $termsArray ) {
151 $terms = implode( '|', $termsArray );
152 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
153 $ret = array_unique( explode('|', $terms) );
154 return $ret;
155 }
156 }
157