842a4805a8de905e155b84574aec569b20ecd349
2 require_once( "includes/ZhClient.php" );
3 require_once( "LanguageZh_cn.php");
4 require_once( "LanguageZh_tw.php");
5 require_once( "LanguageZh_sg.php");
6 require_once( "LanguageZh_hk.php");
8 /* class that handles both Traditional and Simplified Chinese
9 right now it only distinguish zh_cn and zh_tw (actuall, zh_cn and
10 non-zh_cn), will add support for zh_sg, zh_hk, etc, later.
12 class LanguageZh
extends LanguageZh_cn
{
14 var $mZhLanguageCode=false;
16 function LanguageZh() {
17 global $wgUseZhdaemon, $wgZhdaemonHost, $wgZhdaemonPort;
18 global $wgDisableLangConversion, $wgUser;
20 if( $wgUser->getID()!=0 ) {
21 /* allow user to diable conversion */
22 if( $wgDisableLangConversion == false &&
23 $wgUser->getOption('nolangconversion') == 1)
24 $wgDisableLangConversion = true;
27 $this->mZhLanguageCode
= $this->getPreferredVariant();
29 $this->mZhClient
=new ZhClient($wgZhdaemonHost, $wgZhdaemonPort);
30 if(!$this->mZhClient
->isconnected())
31 $this->mZhClient
= false;
33 // fallback to fake client
34 if($this->mZhClient
== false)
35 $this->mZhClient
=new ZhClientFake();
39 get preferred language variants. eventually this will check the
40 user's preference setting as well, once the language option in
41 the setting pages is finalized.
43 function getPreferredVariant() {
46 if($this->mZhLanguageCode
)
47 return $this->mZhLanguageCode
;
49 // get language variant preference for logged in users
50 if($wgUser->getID()!=0) {
51 $this->mZhLanguageCode
= $wgUser->getOption('variant');
54 // see if some zh- variant is set in the http header,
55 $this->mZhLanguageCode
="zh-cn";
56 $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
57 $zh = strstr($header, 'zh-');
59 $this->mZhLanguageCode
= substr($zh,0,5);
62 return $this->mZhLanguageCode
;
65 # this should give much better diff info
66 function segmentForDiff( $text ) {
68 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
69 "' ' .\"$1\"", $text);
72 function unsegmentForDiff( $text ) {
74 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
80 function autoConvert($text, $toVariant=false) {
82 $toVariant = $this->getPreferredVariant();
83 $fname="zhautoConvert";
84 wfProfileIn( $fname );
85 $t = $this->mZhClient
->convert($text, $toVariant);
86 wfProfileOut( $fname );
90 function autoConvertToAllVariants($text) {
91 $fname="zhautoConvertToAll";
92 wfProfileIn( $fname );
93 $ret = $this->mZhClient
->convertToAllVariants($text);
94 if($ret == false) {//fall back...
95 $ret = Language
::autoConvertToAllVariants($text);
97 wfProfileOut( $fname );
101 # only convert titles having more than one character
102 function convertTitle($text) {
104 if( function_exists( 'mb_strlen' ) )
105 $len = mb_strlen($text);
107 $len = strlen($text)/3;
109 return $this->autoConvert( $text);
113 function getVariants() {
114 return array("zh-cn", "zh-tw", "zh-sg", "zh-hk");
117 function getVariantFallback($v) {
119 case 'zh-cn': return 'zh-sg'; break;
120 case 'zh-sg': return 'zh-cn'; break;
121 case 'zh-tw': return 'zh-hk'; break;
122 case 'zh-hk': return 'zh-tw'; break;
127 // word segmentation through ZhClient
128 function stripForSearch( $string ) {
130 wfProfileIn( $fname );
131 //always convert to zh-cn before indexing. it should be
132 //better to use zh-cn for search, since conversion from
133 //Traditional to Simplified is less ambiguous than the
135 $t = $this->mZhClient
->segment($string);
136 $t = $this->autoConvert($t, 'zh-cn');
137 $t = LanguageUtf8
::stripForSearch( $t );
138 wfProfileOut( $fname );
143 function convertForSearchResult( $termsArray ) {
144 $terms = implode( '|', $termsArray );
145 $terms = implode( '|', $this->autoConvertToAllVariants( $terms ) );
146 $ret = array_unique( explode('|', $terms) );
150 function getExtraHashOptions() {
151 return array('variant', 'nolangconversion');