From ca869524aacbc0b047ce8153da6edc7e1aad2970 Mon Sep 17 00:00:00 2001 From: Zheng Zhu Date: Sat, 18 Sep 2004 03:37:50 +0000 Subject: [PATCH] moved parsing of the -{}- tag to Language.php, and generalized it a little bit so that other languages can use it (hopefully). --- languages/Language.php | 52 ++++++++++++++++++++++++++-- languages/LanguageZh.php | 73 ++++++++++------------------------------ 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index e2c3500b23..84895c06ee 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -1958,17 +1958,65 @@ class Language { return $word; } - # Hook for Chinese traditional/simplified conversion + + # convert text to different variants of a language. the automatic + # conversion is done in autoConvert(). here we parse the text + # marked with -{}-, which specifies special conversions of the + # text that can not be accomplished in autoConvert() + # + # syntax of the markup: + # -{code1:text1;code2:text2;...}- or + # -{text}- in which case no conversion should take place for text function convert( $text ) { - return $text; + + $plang = $this->getPreferredVariant(); + if(!$plang) + return $text; + + // no conversion if redirecting + if(substr($text,0,9) == "#REDIRECT") { + return $text; + } + + $tarray = explode("-{", $text); + $tfirst = array_shift($tarray); + $text = $this->autoConvert($tfirst); + + foreach($tarray as $txt) { + $marked = explode("}-", $txt); + + $choice = explode(";", $marked{0}); + if($choice{1}==NULL) { + $text .= $choice{0}; + } + else { + foreach($choice as $c) { + list($code, $content) = split(":", $c); + $code = trim($code); + $content = trim($content); + if($code == $plang) { + $text .= $content; + break; + } + } + } + $text .= $this->autoConvert($marked{1}); + } + + return $text; } + function autoConvert($text) { + return $text; + } + # see if we have a list of language variants for conversion. # right now mainly used in the Chinese conversion function getVariants() { return array(); } + # todo: write general code to get default language variant function getPreferredVariant() { return false; } diff --git a/languages/LanguageZh.php b/languages/LanguageZh.php index 26e14b7488..3f3228fd15 100644 --- a/languages/LanguageZh.php +++ b/languages/LanguageZh.php @@ -33,21 +33,18 @@ class LanguageZh extends LanguageUtf8 { if($this->mZhLanguageCode) return $this->mZhLanguageCode; - /* get language variant preference for logged in users */ + // get language variant preference for logged in users if($wgUser->getID()!=0) { $this->mZhLanguageCode = $wgUser->getOption('variant'); } - else { // see if it is in the http header, otherwise default to zh_cn + else { + // see if some zh- variant is set in the http header, $this->mZhLanguageCode="zh-cn"; - $value = $_SERVER["HTTP_ACCEPT_LANGUAGE"]; - $zh = explode("zh-", $value); - array_shift($zh); - $l = array_shift($zh); - if($l != NULL) { - $this->mZhLanguageCode = "zh-".strtolower(substr($l,0,2)); + $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"])); + $zh = strstr($header, 'zh-'); + if($zh) { + $this->mZhLanguageCode = substr($zh,0,5); } - // also set the variant option of anons - $wgUser->setOption('variant', $this->mZhLanguageCode); } return $this->mZhLanguageCode; } @@ -65,53 +62,17 @@ class LanguageZh extends LanguageUtf8 { return strtr($text, $wgZhTrad2Simp); } - function convert($text) { - - // no conversion if redirecting - if(substr($text,0,9) == "#REDIRECT") { - return $text; - } - - // determine the preferred language from the request header - $tolang = $this->getPreferredVariant(); - - $ltext = explode("-{", $text); - $lfirst = array_shift($ltext); - - if($tolang == "zh-cn") { - $text = $this->trad2simp($lfirst); - } - else { - $text = $this->simp2trad($lfirst); - } - - foreach ($ltext as $txt) { - $a = explode("}-", $txt); - $b = explode("zh-", $a{0}); - if($b{1}==NULL) { - $text = $text.$b{0}; - } - else { - foreach ($b as $lang) { - if(substr($lang,0,2) == substr($tolang,-2)) { - $text = $text.substr($lang, 2); - break; - } - } - } - if($tolang == "zh-cn") { - $text = $text.$this->trad2simp($a{1}); - } - else { - $text = $text.$this->simp2trad($a{1}); - } - } - - return $text; - } - + function autoConvert($text) { + if($this->getPreferredVariant() == "zh-cn") { + return $this->trad2simp($text); + } + else { + return $this->simp2trad($text); + } + } + function getVariants() { - return array("zh_cn", "zh_tw"); + return array("zh-cn", "zh-tw"); } -- 2.20.1