Convert a link to all variants before checking for existence. The reduce the number...
[lhc/web/wiklou.git] / languages / LanguageZh.php
1 <?php
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");
7
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.
11 */
12 class LanguageZh extends LanguageZh_cn {
13
14 var $mZhLanguageCode=false;
15 var $mZhClient=false;
16 function LanguageZh() {
17 global $wgUseZhdaemon, $wgZhdaemonHost, $wgZhdaemonPort;
18 global $wgDisableLangConversion;
19
20 $this->mZhLanguageCode = $this->getPreferredVariant();
21 if($wgUseZhdaemon) {
22 $this->mZhClient=new ZhClient($wgZhdaemonHost, $wgZhdaemonPort);
23 if(!$this->mZhClient->isconnected())
24 $this->mZhClient = false;
25 }
26 // fallback to fake client
27 if($this->mZhClient == false)
28 $this->mZhClient=new ZhClientFake();
29 }
30
31 /*
32 get preferred language variants. eventually this will check the
33 user's preference setting as well, once the language option in
34 the setting pages is finalized.
35 */
36 function getPreferredVariant() {
37 global $wgUser;
38
39 if($this->mZhLanguageCode)
40 return $this->mZhLanguageCode;
41
42 // get language variant preference for logged in users
43 if($wgUser->getID()!=0) {
44 $this->mZhLanguageCode = $wgUser->getOption('variant');
45 }
46 else {
47 // see if some zh- variant is set in the http header,
48 $this->mZhLanguageCode="zh-cn";
49 $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
50 $zh = strstr($header, 'zh-');
51 if($zh) {
52 $this->mZhLanguageCode = substr($zh,0,5);
53 }
54 }
55 return $this->mZhLanguageCode;
56 }
57
58
59
60 function autoConvert($text, $toVariant=false) {
61 if(!$toVariant)
62 $toVariant = $this->getPreferredVariant();
63 $fname="zhautoConvert";
64 wfProfileIn( $fname );
65 $t = $this->mZhClient->convert($text, $toVariant);
66 wfProfileOut( $fname );
67 return $t;
68 }
69
70 function autoConvertToAllVariants($text) {
71 $fname="zhautoConvertToAll";
72 wfProfileIn( $fname );
73 $ret = $this->mZhClient->convertToAllVariants($text);
74 if($ret == false) {//fall back...
75 $ret = Language::autoConvertToAllVariants($text);
76 }
77 wfProfileOut( $fname );
78 return $ret;
79 }
80
81 # only convert titles having more than one character
82 function convertTitle($text) {
83 $len=0;
84 if( function_exists( 'mb_strlen' ) )
85 $len = mb_strlen($text);
86 else
87 $len = strlen($text)/3;
88 if($len>1)
89 return $this->autoConvert( $text);
90 return $text;
91 }
92
93 function getVariants() {
94 return array("zh-cn", "zh-tw", "zh-sg", "zh-hk");
95 }
96
97 function getVariantFallback($v) {
98 switch ($v) {
99 case 'zh-cn': return 'zh-sg'; break;
100 case 'zh-sg': return 'zh-cn'; break;
101 case 'zh-tw': return 'zh-hk'; break;
102 case 'zh-hk': return 'zh-tw'; break;
103 }
104 return false;
105 }
106
107 // word segmentation through ZhClient
108 function stripForSearch( $string ) {
109 $fname="zhsegment";
110 wfProfileIn( $fname );
111 $t = $this->mZhClient->segment($string);
112 wfProfileOut( $fname );
113 return $t;
114
115 }
116 }
117 ?>