User's language variant preference now taking effect
[lhc/web/wiklou.git] / languages / LanguageZh.php
1 <?php
2 require_once( "LanguageUtf8.php" );
3 require_once( "LanguageZh_cn.php");
4 require_once( "LanguageZh_tw.php");
5 require_once( "ZhConversion.php");
6
7 /* class that handles both Traditional and Simplified Chinese
8 right now it only distinguish zh_cn and zh_tw (actuall, zh_cn and
9 non-zh_cn), will add support for zh_sg, zh_hk, etc, later.
10 */
11 class LanguageZh extends LanguageUtf8 {
12
13 var $mZhLang=false, $mZhLanguageCode=false;
14
15 function LanguageZh() {
16 $this->mZhLanguageCode = $this->getPreferredVariant();
17 if($this->mZhLanguageCode == "cn") {
18 $this->mZhLang = new LanguageZh_cn();
19 }
20 else {
21 $this->mZhLang = new LanguageZh_tw();
22 }
23 }
24
25 /*
26 get preferred language variants. eventually this will check the
27 user's preference setting as well, once the language option in
28 the setting pages is finalized.
29 */
30 function getPreferredVariant() {
31 global $wgUser;
32
33 if($this->mZhLanguageCode)
34 return $this->mZhLanguageCode;
35
36 /* get language variant preference for logged in users */
37 if($wgUser->getID()!=0) {
38 $this->mZhLanguageCode = $wgUser->getOption('variant');
39 }
40 else { // see if it is in the http header, otherwise default to zh_cn
41 $this->mZhLanguageCode="zh-cn";
42 $value = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
43 $zh = explode("zh-", $value);
44 array_shift($zh);
45 $l = array_shift($zh);
46 if($l != NULL) {
47 $this->mZhLanguageCode = "zh-".strtolower(substr($l,0,2));
48 }
49 // also set the variant option of anons
50 $wgUser->setOption('variant', $this->mZhLanguageCode);
51 }
52 return $this->mZhLanguageCode;
53 }
54
55
56 /* the Simplified/Traditional conversion stuff */
57
58 function simp2trad($text) {
59 global $wgZhSimp2Trad;
60 return strtr($text, $wgZhSimp2Trad);
61 }
62
63 function trad2simp($text) {
64 global $wgZhTrad2Simp;
65 return strtr($text, $wgZhTrad2Simp);
66 }
67
68 function convert($text) {
69
70 // no conversion if redirecting
71 if(substr($text,0,9) == "#REDIRECT") {
72 return $text;
73 }
74
75 // determine the preferred language from the request header
76 $tolang = $this->getPreferredVariant();
77
78 $ltext = explode("-{", $text);
79 $lfirst = array_shift($ltext);
80
81 if($tolang == "zh-cn") {
82 $text = $this->trad2simp($lfirst);
83 }
84 else {
85 $text = $this->simp2trad($lfirst);
86 }
87
88 foreach ($ltext as $txt) {
89 $a = explode("}-", $txt);
90 $b = explode("zh-", $a{0});
91 if($b{1}==NULL) {
92 $text = $text.$b{0};
93 }
94 else {
95 foreach ($b as $lang) {
96 if(substr($lang,0,2) == substr($tolang,-2)) {
97 $text = $text.substr($lang, 2);
98 break;
99 }
100 }
101 }
102 if($tolang == "zh-cn") {
103 $text = $text.$this->trad2simp($a{1});
104 }
105 else {
106 $text = $text.$this->simp2trad($a{1});
107 }
108 }
109
110 return $text;
111 }
112
113 function getVariants() {
114 return array("zh_cn", "zh_tw");
115 }
116
117
118 /* these just calls the method of the corresponding class */
119
120 function getDefaultUserOptions () {
121 return $this->mZhLang->getDefaultUserOptions();
122 }
123
124 function getBookstoreList () {
125 return $this->mZhLang->getBookstoreList() ;
126 }
127
128 function getNamespaces() {
129 return $this->mZhLang->getNamespaces();
130 }
131
132 function getNsText( $index ) {
133 return $this->mZhLang->getNsText($index);
134 }
135
136 function getNsIndex( $text ) {
137 return $this->mZhLang->getNsIndex($text);
138 }
139
140 function getQuickbarSettings() {
141 return $this->mZhLang->getQuickbarSettings();
142 }
143
144 function getSkinNames() {
145 return $this->mZhLang->getSkinNames();
146 }
147
148 function date( $ts, $adj = false )
149 {
150 return $this->mZhLang->date($ts,$adj);
151 }
152
153 function timeanddate( $ts, $adj = false )
154 {
155 return $this->mZhLang->timeanddate($ts, $adj);
156 }
157
158 function getValidSpecialPages()
159 {
160 return $this->mZhLang->getValidSpecialPages();
161 }
162
163 function getSysopSpecialPages()
164 {
165 return $this->mZhLang->getSysopSpecialPages();
166 }
167
168 function getDeveloperSpecialPages()
169 {
170 return $this->mZhLang->getDeveloperSpecialPages();
171
172 }
173
174 function getMessage( $key )
175 {
176 return $this->mZhLang->getMessage($key);
177 }
178
179 function stripForSearch( $string ) {
180 return $this->mZhLang->stripForSearch($string);
181 }
182
183
184 }
185
186
187 ?>