From cb577df82cecc3e5730c1032fc3734c88806b5f9 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 3 Jul 2010 11:04:15 +0000 Subject: [PATCH] fix language manager to use users choice replace deprecated regexp uses git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@512 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- www/inc/classes/config_manager.php | 4 +- www/inc/classes/language_manager.php | 46 ++++++++++++++++++++------------- www/inc/rendering/event_listing.php | 2 +- www/index.php | 3 -- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/www/inc/classes/config_manager.php b/www/inc/classes/config_manager.php index 8cec61d..05b53a3 100644 --- a/www/inc/classes/config_manager.php +++ b/www/inc/classes/config_manager.php @@ -42,10 +42,10 @@ class ConfigManager $lines = file($this->configFile); $key = ""; foreach ($lines as $line_num => $line) { - if ( ereg("^msgid.*\"(.*)\"", $line, $reg) ) { + if ( preg_match("/^msgid.*\"(.*)\"/", $line, $reg) ) { $key = $reg[1]; } - if ( ereg("^msgstr.*\"(.*)\"", $line, $reg) ) { + if ( preg_match("/^msgstr.*\"(.*)\"/", $line, $reg) ) { $value = $reg[1]; $this->config[$key] = $value; } diff --git a/www/inc/classes/language_manager.php b/www/inc/classes/language_manager.php index 8c37237..8fa2135 100644 --- a/www/inc/classes/language_manager.php +++ b/www/inc/classes/language_manager.php @@ -38,8 +38,8 @@ function string_format($string, $array) class LanguageManager { - private static $instance; - private $lang; + private static $instance=null;//will be instantiated by singleton() + private $lang="C";//fallback for unknown language, constructor overrides it private $config; private $templateFolder; @@ -55,17 +55,34 @@ class LanguageManager //make sure it ends with / if(substr($this->templateFolder,-1,1) != "/") $this->templateFolder .= "/"; - + //collect possible languages for this user + $langs=array(); // check if cookie is set if (isset($_COOKIE[COOKIE_LANGUAGE])) { - $this->lang = $_COOKIE[COOKIE_LANGUAGE]; - } else { - $this->lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2); + $langs[]= $_COOKIE[COOKIE_LANGUAGE]; + } + if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){ + foreach(explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $l){ + //the language itself + $langs[]=trim(strtolower($l)); + //if it has a sub-category, get the parent language + $l2=explode("-",trim($l)); + if(count($l2)<2)continue; + $langs[]=trim(strtolower($l2[0])); + } } - //sanity check for $lang -> must only contain letters; fallback is de - if(ereg("^[a-zA-Z]+$",$this->lang)===false) - $this->lang="de"; + //language sieve: check which ones we know + foreach($langs as $l){ + //sanity check for $lang -> must only contain letters and dashes; fallback is C + if(preg_match("/^[a-zA-Z-]+$/",$l)<1)continue; + //check that we have this directory + if(!file_exists($this->templateFolder . $l))continue; + if(!is_dir($this->templateFolder . $l))continue; + //found it! + $this->lang=$l; + break; + } $this->setLanguageConfig(); } @@ -73,7 +90,7 @@ class LanguageManager /** returns the instance of the Language Manager */ public static function singleton() { - if(!self::$instance) { + if(self::$instance===null) { self::$instance = new LanguageManager(); } @@ -92,15 +109,8 @@ class LanguageManager private function setLanguageConfig() { global $template; - $dir = $this->templateFolder.$this->lang."/"; + $template = $this->templateFolder.$this->lang."/"; - if (is_dir($dir)) { - // if language folder exists - $template = $dir; - } else { - // set default value - $template = $this->templateFolder."de/"; - } $this->config = ConfigManager::singleton("lang.po"); } diff --git a/www/inc/rendering/event_listing.php b/www/inc/rendering/event_listing.php index 6d18f9a..17303ba 100644 --- a/www/inc/rendering/event_listing.php +++ b/www/inc/rendering/event_listing.php @@ -45,7 +45,7 @@ public static function createEventDetails() $error = ErrorManager::singleton(); if (isset($_GET["event"])) { - $eventID = $_GET["event"]; + $eventID = $_GET["event"]+0; } $p = new Parser("eventdetails.html"); diff --git a/www/index.php b/www/index.php index 73af469..cee78d8 100644 --- a/www/index.php +++ b/www/index.php @@ -25,9 +25,6 @@ if(isset($_GET["mode"])){ $mode=$_GET["mode"]; } -//TODO: set this to something more sensible -$lang->setLanguage("de"); - //set internal session to virtual "_web" user for use by transactions Session::setWebSession(); -- 1.7.2.5