From f23f72f28859884f99b4a02687ea60e9d0cdb658 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 4 Nov 2007 20:23:07 +0000 Subject: [PATCH] - changed Config Manager - 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 | 4 +- www/inc/config_manager.php | 40 ++++++++++++++++----- www/inc/error.php | 15 +++++++- www/inc/event.php | 2 +- www/inc/event_listing.php | 4 ++- www/inc/language_manager.php | 80 ++++++++++++++++++++++++++++++------------ www/inc/submit.php | 10 +++--- www/index.php | 4 +- www/template/de/lang.conf | 14 ------- www/template/de/lang.po | 45 +++++++++++++++++++++++ 10 files changed, 158 insertions(+), 60 deletions(-) delete mode 100644 www/template/de/lang.conf create mode 100644 www/template/de/lang.po diff --git a/www/inc/cart_listing.php b/www/inc/cart_listing.php index c598e23..0db1cc8 100644 --- a/www/inc/cart_listing.php +++ b/www/inc/cart_listing.php @@ -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]); diff --git a/www/inc/config_manager.php b/www/inc/config_manager.php index f422fec..52ca045 100644 --- a/www/inc/config_manager.php +++ b/www/inc/config_manager.php @@ -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 */ diff --git a/www/inc/error.php b/www/inc/error.php index 591a4fc..387c8a9 100644 --- a/www/inc/error.php +++ b/www/inc/error.php @@ -12,12 +12,23 @@ 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 */ diff --git a/www/inc/event.php b/www/inc/event.php index 14f9f2e..e6d9db6 100644 --- a/www/inc/event.php +++ b/www/inc/event.php @@ -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()); } diff --git a/www/inc/event_listing.php b/www/inc/event_listing.php index f04c2ca..90444c9 100644 --- a/www/inc/event_listing.php +++ b/www/inc/event_listing.php @@ -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"]; diff --git a/www/inc/language_manager.php b/www/inc/language_manager.php index 6db60e8..cf653b6 100644 --- a/www/inc/language_manager.php +++ b/www/inc/language_manager.php @@ -9,66 +9,100 @@ // +---------------------------------------------------------------------- // +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); } } diff --git a/www/inc/submit.php b/www/inc/submit.php index c646cb2..ed3af6e 100644 --- a/www/inc/submit.php +++ b/www/inc/submit.php @@ -12,20 +12,20 @@ /** 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]); diff --git a/www/index.php b/www/index.php index 89d7d0a..598c606 100644 --- a/www/index.php +++ b/www/index.php @@ -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 index 6a468c0..0000000 --- a/www/template/de/lang.conf +++ /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 index 0000000..0c3b5ba --- /dev/null +++ b/www/template/de/lang.po @@ -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 , 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 \n" +"Language-Team: LANGUAGE \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 "." -- 1.7.2.5