Simplified the LanguageZh class
[lhc/web/wiklou.git] / languages / LanguageZh.php
1 <?php
2 require_once( "LanguageZh_cn.php");
3
4 /* caching the conversion tables */
5 $zhSimp2Trad = $wgMemc->get($key1 = "$wgDBname:zhConvert:s2t");
6 $zhTrad2Simp = $wgMemc->get($key2 = "$wgDBname:zhConvert:t2s");
7 if(empty($zhSimp2Trad) || empty($zhTrad2Simp)) {
8 require_once("includes/ZhConversion.php");
9 $wgMemc->set($key1, $zhSimp2Trad);
10 $wgMemc->set($key2, $zhTrad2Simp);
11 }
12
13 /* class that handles both Traditional and Simplified Chinese
14 right now it only distinguish zh_cn and zh_tw (actuall, zh_cn and
15 non-zh_cn), will add support for zh_sg, zh_hk, etc, later.
16 */
17 class LanguageZh extends LanguageZh_cn {
18
19 var $mZhLanguageCode=false;
20
21 function LanguageZh() {
22 $this->mZhLanguageCode = $this->getPreferredVariant();
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 {
41 // see if some zh- variant is set in the http header,
42 $this->mZhLanguageCode="zh-cn";
43 $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
44 $zh = strstr($header, 'zh-');
45 if($zh) {
46 $this->mZhLanguageCode = substr($zh,0,5);
47 }
48 }
49 return $this->mZhLanguageCode;
50 }
51
52
53 /* the Simplified/Traditional conversion stuff */
54
55 function simp2trad($text) {
56 global $zhSimp2Trad;
57 return strtr($text, $zhSimp2Trad);
58 }
59
60 function trad2simp($text) {
61 global $zhTrad2Simp;
62 return strtr($text, $zhTrad2Simp);
63 }
64
65 function autoConvert($text) {
66 if($this->getPreferredVariant() == "zh-cn") {
67 return $this->trad2simp($text);
68 }
69 else {
70 return $this->simp2trad($text);
71 }
72 }
73
74 function getVariants() {
75 return array("zh-cn", "zh-tw");
76 }
77 }
78 ?>