- changed Config Manager
authorpeter <peter@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 4 Nov 2007 20:23:07 +0000 (20:23 +0000)
committerpeter <peter@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 4 Nov 2007 20:23:07 +0000 (20:23 +0000)
- added i18n functionality
- made Config, Language and Error Manager to singletons

git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@67 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

www/inc/cart_listing.php
www/inc/config_manager.php
www/inc/error.php
www/inc/event.php
www/inc/event_listing.php
www/inc/language_manager.php
www/inc/submit.php
www/index.php
www/template/de/lang.conf [deleted file]
www/template/de/lang.po [new file with mode: 0644]

index c598e23..0db1cc8 100644 (file)
@@ -12,9 +12,9 @@
 function createCartOverview()
 {
        global $parser;
-       global $error;
-       global $lang;
        
+       $error = ErrorManager::singleton();
+       $lang = LanguageManager::singleton();
        $COOKIE_NAME = "ms_cartid";
        
        $cart = new Cart($_COOKIE[$COOKIE_NAME]);
index f422fec..52ca045 100644 (file)
@@ -13,8 +13,9 @@ class ConfigManager
 {
        private $configFile;
        private $config;
+       private static $instance;
        
-       public function __construct($file)
+       private function __construct($file)
        {
                global $template;
                
@@ -22,6 +23,16 @@ class ConfigManager
                $this->readConfig();
        }
        
+       /** returns the instance of the Config Manager */
+       public static function singleton($file)
+       {
+               if(!self::$instance) {
+               self::$instance = new ConfigManager($file);
+       } 
+       
+       return self::$instance;
+       }
+       
        /** reads the configuration values from the file */
        private function readConfig()
        {
@@ -29,15 +40,14 @@ class ConfigManager
                if (file_exists($this->configFile)) {
                        $lines = file($this->configFile);
                        $this->config = array();
-       
+                       $key = "";
                        foreach ($lines as $line_num => $line) {
-                               # Comment?
-                               if ( !preg_match("/^#.*/", $line) ) {
-                                       # Contains non-whitespace?
-                                       if ( preg_match("/\S/", $line) ) {
-                                               list( $key, $value ) = explode( "=", trim( $line ), 2);
-                                               $this->config[trim($key)] = trim($value);
-                                       }
+                               if ( ereg("^msgid.*\"(.*)\"", $line, $reg) ) {
+                                       $key = $reg[1];
+                               }
+                               if ( ereg("^msgstr.*\"(.*)\"", $line, $reg) ) {
+                                       $value = $reg[1];
+                                       $this->config[$key] = $value;
                                }
                        }
                }
@@ -46,7 +56,17 @@ class ConfigManager
        /** returns the value of the given configuration item */
        public function get($key)
        {
-               return $this->config[$key];
+               if ($this->hasKey($key)) {
+                       return $this->config[$key];
+               } else {
+                       return "";
+               }
+       }
+
+       /** checks if key exists */
+       public function hasKey($key)
+       {
+               return array_key_exists($key, $this->config);
        }
        
        /** can be used to set an alternate path to a config file */
index 591a4fc..387c8a9 100644 (file)
 class ErrorManager
 {
        private $errorMessages;
+       private static $instance;
        
-       public function __construct()
+       private function __construct()
        {
                $this->errorMessages = array(); 
        }
        
+       /** returns the instance of the Error Manager */
+       public static function singleton()
+       {
+               if(!self::$instance) {
+               self::$instance = new ErrorManager();
+       }
+       
+       return self::$instance;
+       }
+       
        /** add new error message */
        public function add($message)
        {
@@ -27,7 +38,7 @@ class ErrorManager
        /** get all error messages in an array */
        public function getAll()
        {
-               return $this->errorMessages;
+               return $this->$errorMessages;
        }
        
        /** get all error messages formatted */
index 14f9f2e..e6d9db6 100644 (file)
@@ -119,7 +119,7 @@ class Event
        /**returns the data in an array*/
        public function getParserData()
        {
-               global $lang;
+               $lang = LanguageManager::singleton();
        
                return array("DATE"=>$lang->getDate($this->getStartTime()), "TIME"=>$lang->getTime($this->getStartTime()), "PLACE"=>$this->getRoomId(), "EVENTNAME"=>$this->getTitle(), "ARTIST"=>$this->getArtist(),"PRICE"=>$lang->getPrice($this->getDefaultPrice()), "ID"=>$this->getEventId(), "DESCRIPTION"=>$this->getDescription());
        }
index f04c2ca..90444c9 100644 (file)
@@ -12,6 +12,7 @@
 function createEventList()
 {
        global $parser;
+       
        //pass 1: get layout of single event
        $p=new Parser("index.html");
        $list="";
@@ -33,7 +34,8 @@ function createEventList()
 function createEventDetails()
 {
        global $parser;
-       global $error;
+       
+       $error = ErrorManager::singleton();
 
        if (isset($_GET["event"])) {
                $eventID = $_GET["event"];
index 6db60e8..cf653b6 100644 (file)
 // +----------------------------------------------------------------------
 //
 
+function i18n($key)
+{
+       $lang = LanguageManager::singleton();
+       
+       $translation = $lang->getValue($key);
+       
+       if ($translation != "") {
+               return $translation;
+       } else {
+               return $key;
+       }
+}
+
 class LanguageManager
 {
-       private $COOKIE_NAME = "ms_lang"; 
+       private static $COOKIE_NAME = "ms_lang";
+       private static $instance;
        private $lang;
        private $config;
+       private $templateFolder;
        
-       /** constructor */
-       public function __construct()
-       {
+       /** private constructor */
+       private function __construct()
+       {       
                global $template;
                
+               $this->templateFolder = $template;              
+       
                // check if cookie is set
-               if (isset($_COOKIE[$this->COOKIE_NAME])) {
-                       $this->lang = $_COOKIE[$this->COOKIE_NAME];
+               if (isset($_COOKIE[self::$COOKIE_NAME])) {
+                       $this->lang = $_COOKIE[self::$COOKIE_NAME];
                } else {
                        $this->lang = substr($_SERVER[HTTP_ACCEPT_LANGUAGE],0,2);
                }
                
-               $dir = $template.$this->lang."/";
+               $this->setLanguageConfig();
+       }
+       
+       /** returns the instance of the Language Manager */
+       public static function singleton()
+       {
+               if(!self::$instance) {
+               self::$instance = new LanguageManager();
+       }
+       
+       return self::$instance;
+       }
+       
+       /** set language */
+       public function setLanguage($language)
+       {
+               $this->lang = $language;
+               setcookie(self::$COOKIE_NAME, $language, 0);
+               
+               $this->setLanguageConfig();
+       }
+       
+       private function setLanguageConfig() {
+               global $template;
+               
+               $dir = $this->templateFolder.$this->lang."/";
                
                if (is_dir($dir)) {
                        // if language folder exists
                        $template = $dir;
                } else {
                        // set default value
-                       $template .= "de/";
+                       $template = $this->templageFolder."de/";
                }
-               
-               $this->config = new ConfigManager("lang.conf");
-       }
-       
-       /** set language */
-       public function setLanguage($language)
-       {
-               $this->lang = $language;
-               setcookie($this->COOKIE_NAME, $language, 0);
+               $this->config = ConfigManager::singleton("lang.po");
        }
        
        /** returns date in current language */
        public function getDate($date)
        {
-               return date($this->config->get("DateFormat"), $date);
+               return date(i18n("d/m/Y"), $date);
        }
        
        /** returns time in current language */
        public function getTime($time)
        {
-               return date($this->config->get("TimeFormat"), $time);
+               return date(i18n("h:i a"), $time);
        }
        
        /** returns price in current language */
        public function getPrice($price)
        {
-               return number_format($price/100, 2, $this->config->get("DecimalPoint"), $this->config->get("ThousandSeparator"));
+               return number_format($price/100, 2, i18n("."), i18n(","));
        }
        
-       /** returns error message for specified number in current language */
-       public function getErrMsg($num)
+       /** returns value for specified key in current language */
+       public function getValue($key)
        {
-               return $this->config->get("ERR".$num);
+               return $this->config->get($key);
        }
 }
 
index c646cb2..ed3af6e 100644 (file)
 /** adds an event to the cart */ 
 function addEventToCart()
 {
-       global $error;
-       global $lang;
+       $error = ErrorManager::singleton();
+       $lang = LanguageManager::singleton();
        $COOKIE_NAME = "ms_cartid";
 
        if (isset($_POST["ms_save"])) {
                $event = new Event($_GET["event"]);
                if (empty($_POST["ms_amount"])) {
-                       $error->add($lang->getErrMsg("100"));
+                       $error->add(i18n("Please insert the number of tickets!"));
                        return;
                } elseif (!is_numeric($_POST["ms_amount"])) {
-                       $error->add($lang->getErrMsg("000"));
+                       $error->add(i18n("Please insert a number!"));
                        return;
                } elseif (!$event->exists()) {
-                       $error->add($lang->getErrMsg("200"));
+                       $error->add(i18n("The event does not exist!"));
                        return;
                } else {
                        $cart = new Cart($_COOKIE[$COOKIE_NAME]);
index 89d7d0a..598c606 100644 (file)
@@ -16,9 +16,9 @@ if(isset($_GET["mode"])){
        $mode=$_GET["mode"];
 }
 
+$lang = LanguageManager::singleton();
+$error = ErrorManager::singleton();
 $parser = new Parser();
-$error = new ErrorManager();
-$lang = new LanguageManager();
 
 //get page template and process it
 switch($mode){
diff --git a/www/template/de/lang.conf b/www/template/de/lang.conf
deleted file mode 100644 (file)
index 6a468c0..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-# Time
-TimeFormat = H:i 
-DateFormat = d/m/Y
-
-# Currency
-DecimalPoint = .
-ThousandSeparator = ,
-
-# Error messages
-ERR000 = Bitte geben Sie eine Zahl ein!
-
-ERR100 = Bitte geben Sie eine Kartenmenge an!
-
-ERR200 = Veranstaltung nicht vorhanden!
\ No newline at end of file
diff --git a/www/template/de/lang.po b/www/template/de/lang.po
new file mode 100644 (file)
index 0000000..0c3b5ba
--- /dev/null
@@ -0,0 +1,45 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2007-11-04 17:12+0100\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=CHARSET\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: ../../inc/submit.php:22
+msgid "Please insert the number of tickets!"
+msgstr "Please insert the number of tickets!"
+
+#: ../../inc/submit.php:25
+msgid "Please insert a number!"
+msgstr "Please insert a number!"
+
+#: ../../inc/submit.php:28
+msgid "The event does not exist!"
+msgstr "The event does not exist!"
+
+#: inc/language_manager.php:60
+msgid "d/m/Y"
+msgstr "d.m.Y"
+
+#: inc/language_manager.php:66
+msgid "h:i a"
+msgstr "H:i"
+
+#: inc/language_manager.php:72
+msgid "."
+msgstr ","
+
+#: inc/language_manager.php:72
+msgid ","
+msgstr "."