Index: trunk/phase3/languages/Language.php |
— | — | @@ -1958,17 +1958,65 @@ |
1959 | 1959 | return $word; |
1960 | 1960 | } |
1961 | 1961 | |
1962 | | - # Hook for Chinese traditional/simplified conversion |
| 1962 | + |
| 1963 | + # convert text to different variants of a language. the automatic |
| 1964 | + # conversion is done in autoConvert(). here we parse the text |
| 1965 | + # marked with -{}-, which specifies special conversions of the |
| 1966 | + # text that can not be accomplished in autoConvert() |
| 1967 | + # |
| 1968 | + # syntax of the markup: |
| 1969 | + # -{code1:text1;code2:text2;...}- or |
| 1970 | + # -{text}- in which case no conversion should take place for text |
1963 | 1971 | function convert( $text ) { |
1964 | | - return $text; |
| 1972 | + |
| 1973 | + $plang = $this->getPreferredVariant(); |
| 1974 | + if(!$plang) |
| 1975 | + return $text; |
| 1976 | + |
| 1977 | + // no conversion if redirecting |
| 1978 | + if(substr($text,0,9) == "#REDIRECT") { |
| 1979 | + return $text; |
| 1980 | + } |
| 1981 | + |
| 1982 | + $tarray = explode("-{", $text); |
| 1983 | + $tfirst = array_shift($tarray); |
| 1984 | + $text = $this->autoConvert($tfirst); |
| 1985 | + |
| 1986 | + foreach($tarray as $txt) { |
| 1987 | + $marked = explode("}-", $txt); |
| 1988 | + |
| 1989 | + $choice = explode(";", $marked{0}); |
| 1990 | + if($choice{1}==NULL) { |
| 1991 | + $text .= $choice{0}; |
| 1992 | + } |
| 1993 | + else { |
| 1994 | + foreach($choice as $c) { |
| 1995 | + list($code, $content) = split(":", $c); |
| 1996 | + $code = trim($code); |
| 1997 | + $content = trim($content); |
| 1998 | + if($code == $plang) { |
| 1999 | + $text .= $content; |
| 2000 | + break; |
| 2001 | + } |
| 2002 | + } |
| 2003 | + } |
| 2004 | + $text .= $this->autoConvert($marked{1}); |
| 2005 | + } |
| 2006 | + |
| 2007 | + return $text; |
1965 | 2008 | } |
1966 | 2009 | |
| 2010 | + function autoConvert($text) { |
| 2011 | + return $text; |
| 2012 | + } |
| 2013 | + |
1967 | 2014 | # see if we have a list of language variants for conversion. |
1968 | 2015 | # right now mainly used in the Chinese conversion |
1969 | 2016 | function getVariants() { |
1970 | 2017 | return array(); |
1971 | 2018 | } |
1972 | 2019 | |
| 2020 | + # todo: write general code to get default language variant |
1973 | 2021 | function getPreferredVariant() { |
1974 | 2022 | return false; |
1975 | 2023 | } |
Index: trunk/phase3/languages/LanguageZh.php |
— | — | @@ -33,21 +33,18 @@ |
34 | 34 | if($this->mZhLanguageCode) |
35 | 35 | return $this->mZhLanguageCode; |
36 | 36 | |
37 | | - /* get language variant preference for logged in users */ |
| 37 | + // get language variant preference for logged in users |
38 | 38 | if($wgUser->getID()!=0) { |
39 | 39 | $this->mZhLanguageCode = $wgUser->getOption('variant'); |
40 | 40 | } |
41 | | - else { // see if it is in the http header, otherwise default to zh_cn |
| 41 | + else { |
| 42 | + // see if some zh- variant is set in the http header, |
42 | 43 | $this->mZhLanguageCode="zh-cn"; |
43 | | - $value = $_SERVER["HTTP_ACCEPT_LANGUAGE"]; |
44 | | - $zh = explode("zh-", $value); |
45 | | - array_shift($zh); |
46 | | - $l = array_shift($zh); |
47 | | - if($l != NULL) { |
48 | | - $this->mZhLanguageCode = "zh-".strtolower(substr($l,0,2)); |
| 44 | + $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"])); |
| 45 | + $zh = strstr($header, 'zh-'); |
| 46 | + if($zh) { |
| 47 | + $this->mZhLanguageCode = substr($zh,0,5); |
49 | 48 | } |
50 | | - // also set the variant option of anons |
51 | | - $wgUser->setOption('variant', $this->mZhLanguageCode); |
52 | 49 | } |
53 | 50 | return $this->mZhLanguageCode; |
54 | 51 | } |
— | — | @@ -65,53 +62,17 @@ |
66 | 63 | return strtr($text, $wgZhTrad2Simp); |
67 | 64 | } |
68 | 65 | |
69 | | - function convert($text) { |
| 66 | + function autoConvert($text) { |
| 67 | + if($this->getPreferredVariant() == "zh-cn") { |
| 68 | + return $this->trad2simp($text); |
| 69 | + } |
| 70 | + else { |
| 71 | + return $this->simp2trad($text); |
| 72 | + } |
| 73 | + } |
70 | 74 | |
71 | | - // no conversion if redirecting |
72 | | - if(substr($text,0,9) == "#REDIRECT") { |
73 | | - return $text; |
74 | | - } |
75 | | - |
76 | | - // determine the preferred language from the request header |
77 | | - $tolang = $this->getPreferredVariant(); |
78 | | - |
79 | | - $ltext = explode("-{", $text); |
80 | | - $lfirst = array_shift($ltext); |
81 | | - |
82 | | - if($tolang == "zh-cn") { |
83 | | - $text = $this->trad2simp($lfirst); |
84 | | - } |
85 | | - else { |
86 | | - $text = $this->simp2trad($lfirst); |
87 | | - } |
88 | | - |
89 | | - foreach ($ltext as $txt) { |
90 | | - $a = explode("}-", $txt); |
91 | | - $b = explode("zh-", $a{0}); |
92 | | - if($b{1}==NULL) { |
93 | | - $text = $text.$b{0}; |
94 | | - } |
95 | | - else { |
96 | | - foreach ($b as $lang) { |
97 | | - if(substr($lang,0,2) == substr($tolang,-2)) { |
98 | | - $text = $text.substr($lang, 2); |
99 | | - break; |
100 | | - } |
101 | | - } |
102 | | - } |
103 | | - if($tolang == "zh-cn") { |
104 | | - $text = $text.$this->trad2simp($a{1}); |
105 | | - } |
106 | | - else { |
107 | | - $text = $text.$this->simp2trad($a{1}); |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - return $text; |
112 | | - } |
113 | | - |
114 | 75 | function getVariants() { |
115 | | - return array("zh_cn", "zh_tw"); |
| 76 | + return array("zh-cn", "zh-tw"); |
116 | 77 | } |
117 | 78 | |
118 | 79 | |