r5287 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r5286‎ | r5287 | r5288 >
Date:03:37, 18 September 2004
Author:zhengzhu
Status:old
Tags:
Comment:
moved parsing of the -{}- tag to Language.php, and generalized it a little bit so that other languages can use it (hopefully).
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)
  • /trunk/phase3/languages/LanguageZh.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/Language.php
@@ -1958,17 +1958,65 @@
19591959 return $word;
19601960 }
19611961
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
19631971 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;
19652008 }
19662009
 2010+ function autoConvert($text) {
 2011+ return $text;
 2012+ }
 2013+
19672014 # see if we have a list of language variants for conversion.
19682015 # right now mainly used in the Chinese conversion
19692016 function getVariants() {
19702017 return array();
19712018 }
19722019
 2020+ # todo: write general code to get default language variant
19732021 function getPreferredVariant() {
19742022 return false;
19752023 }
Index: trunk/phase3/languages/LanguageZh.php
@@ -33,21 +33,18 @@
3434 if($this->mZhLanguageCode)
3535 return $this->mZhLanguageCode;
3636
37 - /* get language variant preference for logged in users */
 37+ // get language variant preference for logged in users
3838 if($wgUser->getID()!=0) {
3939 $this->mZhLanguageCode = $wgUser->getOption('variant');
4040 }
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,
4243 $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);
4948 }
50 - // also set the variant option of anons
51 - $wgUser->setOption('variant', $this->mZhLanguageCode);
5249 }
5350 return $this->mZhLanguageCode;
5451 }
@@ -65,53 +62,17 @@
6663 return strtr($text, $wgZhTrad2Simp);
6764 }
6865
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+ }
7074
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 -
11475 function getVariants() {
115 - return array("zh_cn", "zh_tw");
 76+ return array("zh-cn", "zh-tw");
11677 }
11778
11879

Status & tagging log