| 1 | <?php
 | 
  
    | 2 | 
 | 
  
    | 3 | /**
 | 
  
    | 4 |  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 | 
  
    | 5 |  *
 | 
  
    | 6 |  * This program is free software: you can redistribute it and/or modify
 | 
  
    | 7 |  * it under the terms of the GNU General Public License as published by
 | 
  
    | 8 |  * the Free Software Foundation, either version 3 of the License, or
 | 
  
    | 9 |  * (at your option) any later version.
 | 
  
    | 10 |  *
 | 
  
    | 11 |  * This program is distributed in the hope that it will be useful,
 | 
  
    | 12 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
  
    | 13 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
  
    | 14 |  * GNU General Public License for more details.
 | 
  
    | 15 |  *
 | 
  
    | 16 |  * You should have received a copy of the GNU General Public License
 | 
  
    | 17 |  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
  
    | 18 |  */
 | 
  
    | 19 | 
 | 
  
    | 20 | /**
 | 
  
    | 21 |  * Translate.php
 | 
  
    | 22 |  *
 | 
  
    | 23 |  * @category     Core
 | 
  
    | 24 |  * @package      Core_Translation
 | 
  
    | 25 |  * @copyright    Werner v.d.Decken <wkl@isteam.de>
 | 
  
    | 26 |  * @author       Werner v.d.Decken <wkl@isteam.de>
 | 
  
    | 27 |  * @license      http://www.gnu.org/licenses/gpl.html   GPL License
 | 
  
    | 28 |  * @version      0.0.1
 | 
  
    | 29 |  * @revision     $Revision: 1902 $
 | 
  
    | 30 |  * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/Translate.php $
 | 
  
    | 31 |  * @lastmodified $Date: 2013-05-12 16:19:33 +0200 (Sun, 12 May 2013) $
 | 
  
    | 32 |  * @since        File available since 12.01.2013
 | 
  
    | 33 |  * @description  basic class of the Translate package
 | 
  
    | 34 |  */
 | 
  
    | 35 | class Translate {
 | 
  
    | 36 | 	
 | 
  
    | 37 | /** holds the active singleton instance */
 | 
  
    | 38 | 	private static $_oInstance     = null;
 | 
  
    | 39 | /** name of the default adaptor */	
 | 
  
    | 40 | 	protected $sAdaptor            = 'WbOldStyle';
 | 
  
    | 41 | /** setting for the system language (default: en) */
 | 
  
    | 42 | 	protected $sSystemLanguage     = 'en';
 | 
  
    | 43 | /** setting for the default runtime language (default: en) */
 | 
  
    | 44 | 	protected $sDefaultLanguage    = 'en';
 | 
  
    | 45 | /** setting for the user defined language (default: en) */
 | 
  
    | 46 | 	protected $sUserLanguage       = 'en';
 | 
  
    | 47 | /** switch cache on/off */
 | 
  
    | 48 | 	protected $bUseCache           = true;
 | 
  
    | 49 | /** defines if the keys of undefines translation should be preserved */
 | 
  
    | 50 | 	protected $bRemoveMissing      = false;
 | 
  
    | 51 | /** can hold up to 31 boolean option settings */
 | 
  
    | 52 | 	protected $iOptions            = 0;
 | 
  
    | 53 | /** List of loaded translation tables */
 | 
  
    | 54 | 	protected $aLoadedAddons       = array();
 | 
  
    | 55 | /** TranslationTable object of the core and additional one activated addon */	
 | 
  
    | 56 | 	protected $aActiveTranslations = array();
 | 
  
    | 57 | /** possible option flags */	
 | 
  
    | 58 | 	const CACHE_DISABLED = 1; // ( 2^0 )
 | 
  
    | 59 | 	const KEEP_MISSING   = 2; // ( 2^1 )
 | 
  
    | 60 | 	
 | 
  
    | 61 | /** prevent class from public instancing and get an object to hold extensions */
 | 
  
    | 62 | 	protected function  __construct() {}
 | 
  
    | 63 | /** prevent from cloning existing instance */
 | 
  
    | 64 | 	private function __clone() {}
 | 
  
    | 65 | /**
 | 
  
    | 66 |  * get a valid instance of this class
 | 
  
    | 67 |  * @return object
 | 
  
    | 68 |  */
 | 
  
    | 69 | 	static public function getInstance() {
 | 
  
    | 70 | 		if( is_null(self::$_oInstance) ) {
 | 
  
    | 71 |             $c = __CLASS__;
 | 
  
    | 72 |             self::$_oInstance = new $c;
 | 
  
    | 73 | 		}
 | 
  
    | 74 | 		return self::$_oInstance;
 | 
  
    | 75 | 	}
 | 
  
    | 76 | /**
 | 
  
    | 77 |  * Initialize the Translations
 | 
  
    | 78 |  * @param string SystemLanguage code ('de' || 'de_CH' || 'de_CH_uri')
 | 
  
    | 79 |  * @param string DefaultLanguage code ('de' || 'de_CH' || 'de_CH_uri')
 | 
  
    | 80 |  * @param string UserLanguage code ('de' || 'de_CH' || 'de_CH_uri')
 | 
  
    | 81 |  * @param string Name of the adaptor to use
 | 
  
    | 82 |  * @param integer possible options (DISABLE_CACHE | KEEP_MISSING)
 | 
  
    | 83 |  * @throws TranslationException
 | 
  
    | 84 |  */
 | 
  
    | 85 | 	public function initialize($sSystemLanguage, 
 | 
  
    | 86 | 	                           $sDefaultLanguage, 
 | 
  
    | 87 | 	                           $sUserLanguage = '', 
 | 
  
    | 88 | 	                           $sAdaptor = 'WbOldStyle', 
 | 
  
    | 89 | 	                           $iOptions = 0)
 | 
  
    | 90 | 	{
 | 
  
    | 91 | 		if(!class_exists('TranslateAdaptor'.$sAdaptor)) {
 | 
  
    | 92 | 			throw new TranslationException('unable to load adaptor: '.$sAdaptor);
 | 
  
    | 93 | 		}
 | 
  
    | 94 | 		$this->sAdaptor       = 'TranslateAdaptor'.$sAdaptor;
 | 
  
    | 95 | 		$this->bUseCache      = (!($iOptions & self::CACHE_DISABLED));
 | 
  
    | 96 | 		$this->bRemoveMissing = (!($iOptions & self::KEEP_MISSING));
 | 
  
    | 97 | 		// if no system language is set then use language 'en'
 | 
  
    | 98 | 		$this->sSystemLanguage = (trim($sSystemLanguage) == '' ? 'en' : $sSystemLanguage);
 | 
  
    | 99 | 		// if no default language is set then use system language
 | 
  
    | 100 | 		$this->sDefaultLanguage = (trim($sDefaultLanguage) == '' 
 | 
  
    | 101 | 		                            ? $this->sSystemLanguage 
 | 
  
    | 102 | 		                            : $sDefaultLanguage);
 | 
  
    | 103 | 		// if no user language is set then use default language
 | 
  
    | 104 | 		$this->sUserLanguage = (trim($sUserLanguage) == '' 
 | 
  
    | 105 | 		                         ? $this->sDefaultLanguage
 | 
  
    | 106 | 		                         : $sUserLanguage);
 | 
  
    | 107 | 		$sPattern = '/^[a-z]{2,3}(?:(?:[_-][a-z]{2})?(?:[_-][a-z0-9]{2,4})?)$/siU';
 | 
  
    | 108 | 		// validate language codes
 | 
  
    | 109 | 		if(preg_match($sPattern, $this->sSystemLanguage) &&
 | 
  
    | 110 | 		   preg_match($sPattern, $this->sDefaultLanguage) &&
 | 
  
    | 111 | 		   preg_match($sPattern, $this->sUserLanguage))
 | 
  
    | 112 | 		{
 | 
  
    | 113 | 		// load core translations and activate it
 | 
  
    | 114 | 			$oTmp = new TranslationTable('', 
 | 
  
    | 115 | 			                             $this->sSystemLanguage, 
 | 
  
    | 116 | 			                             $this->sDefaultLanguage, 
 | 
  
    | 117 | 			                             $this->sUserLanguage,
 | 
  
    | 118 | 			                             $this->bUseCache);
 | 
  
    | 119 | 			$this->aLoadedAddons['core'] = $oTmp->load($this->sAdaptor);
 | 
  
    | 120 | 			$this->aActiveTranslations[0] = $this->aLoadedAddons['core'];
 | 
  
    | 121 | 			if(sizeof($this->aLoadedAddons['core']) == 0) {
 | 
  
    | 122 | 			// throw an exception for missing translations
 | 
  
    | 123 | 				throw new TranslationException('missing core translations');
 | 
  
    | 124 | 			}
 | 
  
    | 125 | 		}else {
 | 
  
    | 126 | 		// throw an exception for invalid or missing language codes
 | 
  
    | 127 | 			$sMsg = 'Invalid language codes: ['.$this->sSystemLanguage.'] or ['
 | 
  
    | 128 | 			      . $this->sDefaultLanguage.'] or ['.$this->sUserLanguage.']';
 | 
  
    | 129 | 			throw new TranslationException($sMsg);
 | 
  
    | 130 | 		}
 | 
  
    | 131 | 	}
 | 
  
    | 132 | /**
 | 
  
    | 133 |  * Add new addon
 | 
  
    | 134 |  * @param string Addon descriptor (i.e. 'modules\myAddon')
 | 
  
    | 135 |  * @param string (optional)Adaptor name (default: $this->sAdaptor)
 | 
  
    | 136 |  * @return bool 
 | 
  
    | 137 |  */	
 | 
  
    | 138 | 	public function addAddon($sAddon, $sAdaptor = null)
 | 
  
    | 139 | 	{
 | 
  
    | 140 | 		if($sAdaptor) {
 | 
  
    | 141 | 			if(!class_exists('TranslateAdaptor'.$sAdaptor)) {
 | 
  
    | 142 | 				throw new TranslationException('unable to load adaptor: '.$sAdaptor);
 | 
  
    | 143 | 			}
 | 
  
    | 144 | 			$sAdaptor = 'TranslateAdaptor'.$sAdaptor;
 | 
  
    | 145 | 		}else {
 | 
  
    | 146 | 			$sAdaptor = $this->sAdaptor;
 | 
  
    | 147 | 		}
 | 
  
    | 148 | 		$sAddon = str_replace('/', '\\', $sAddon);
 | 
  
    | 149 | 		if(!(strtolower($sAddon) == 'core' || $sAddon == '' || isset($this->aLoadedAddons[$sAddon]))) {
 | 
  
    | 150 | 		// load requested addon translations if needed and possible
 | 
  
    | 151 | 			$oTmp = new TranslationTable($sAddon, 
 | 
  
    | 152 | 			                             $this->sSystemLanguage, 
 | 
  
    | 153 | 			                             $this->sDefaultLanguage, 
 | 
  
    | 154 | 			                             $this->sUserLanguage,
 | 
  
    | 155 | 			                             $this->bUseCache);
 | 
  
    | 156 | 			$this->aLoadedAddons[$sAddon] = $oTmp->load($sAdaptor);
 | 
  
    | 157 | 		}
 | 
  
    | 158 | 	}
 | 
  
    | 159 | /**
 | 
  
    | 160 |  * Activate Addon
 | 
  
    | 161 |  * @param string Addon descriptor (i.e. 'modules\myAddon')
 | 
  
    | 162 |  * @param string (optional)Adaptor name (default: $this->sAdaptor)
 | 
  
    | 163 |  */	
 | 
  
    | 164 | 	public function enableAddon($sAddon, $sAdaptor = null)
 | 
  
    | 165 | 	{
 | 
  
    | 166 | 		$sAddon = str_replace('/', '\\', $sAddon);
 | 
  
    | 167 | 		if(!(strtolower($sAddon) == 'core' || $sAddon == '')) {
 | 
  
    | 168 | 			if(!isset($this->aLoadedAddons[$sAddon])) {
 | 
  
    | 169 | 				$this->addAddon($sAddon, $sAdaptor);
 | 
  
    | 170 | 			}
 | 
  
    | 171 | 			$this->aActiveTranslations[1] = $this->aLoadedAddons[$sAddon];
 | 
  
    | 172 | 		}
 | 
  
    | 173 | 		
 | 
  
    | 174 | 	}
 | 
  
    | 175 | /**
 | 
  
    | 176 |  * Dissable active addon
 | 
  
    | 177 |  */	
 | 
  
    | 178 | 	public function disableAddon()
 | 
  
    | 179 | 	{
 | 
  
    | 180 | 		if(isset($this->aActiveTranslations[1])) {
 | 
  
    | 181 | 			unset($this->aActiveTranslations[1]);
 | 
  
    | 182 | 		}
 | 
  
    | 183 | 	}
 | 
  
    | 184 | 	
 | 
  
    | 185 | /**
 | 
  
    | 186 |  * Is key available
 | 
  
    | 187 |  * @param string Language key
 | 
  
    | 188 |  * @return bool
 | 
  
    | 189 |  */
 | 
  
    | 190 | 	public function __isset($sKey)
 | 
  
    | 191 | 	{
 | 
  
    | 192 | 		foreach($this->aActiveTranslations as $oAddon) {
 | 
  
    | 193 | 			if(isset($oAddon->$sKey)) {
 | 
  
    | 194 | 			// is true if at least one translation is found
 | 
  
    | 195 | 				return true;
 | 
  
    | 196 | 			}
 | 
  
    | 197 | 		}
 | 
  
    | 198 | 		return false;
 | 
  
    | 199 | 	}
 | 
  
    | 200 | /**
 | 
  
    | 201 |  * Get translation text
 | 
  
    | 202 |  * @param string Language key
 | 
  
    | 203 |  * @return string Translation text
 | 
  
    | 204 |  */	
 | 
  
    | 205 | 	public function __get($sKey)
 | 
  
    | 206 | 	{
 | 
  
    | 207 | 		$sRetval = ($this->bRemoveMissing ? '' : '#'.$sKey.'#');
 | 
  
    | 208 | 		foreach($this->aActiveTranslations as $oAddon) {
 | 
  
    | 209 | 			if(isset($oAddon->$sKey)) {
 | 
  
    | 210 | 			// search the last matching translation (Core -> Addon)
 | 
  
    | 211 | 				$sRetval = $oAddon->$sKey;
 | 
  
    | 212 | 			}
 | 
  
    | 213 | 		}
 | 
  
    | 214 | 		return $sRetval;
 | 
  
    | 215 | 	}
 | 
  
    | 216 | /**
 | 
  
    | 217 |  * Protect class from property injections
 | 
  
    | 218 |  * @param string name of property
 | 
  
    | 219 |  * @param mixed value
 | 
  
    | 220 |  * @throws TranslationException
 | 
  
    | 221 |  */	
 | 
  
    | 222 | 	public function __set($name, $value) {
 | 
  
    | 223 | 		throw new TranslationException('tried to set a readonly or nonexisting property ['.$name.']!! ');
 | 
  
    | 224 | 	}
 | 
  
    | 225 | /**
 | 
  
    | 226 |  * Return complete table of translations
 | 
  
    | 227 |  * @return array
 | 
  
    | 228 |  * @deprecated for backward compatibility only. Will be removed shortly
 | 
  
    | 229 |  */	
 | 
  
    | 230 | 	public function getLangArray()
 | 
  
    | 231 | 	{
 | 
  
    | 232 | 		$aSumTranslations = array();
 | 
  
    | 233 | 		foreach($this->aActiveTranslations as $oTranslationTable) {
 | 
  
    | 234 | 			if(get_class($oTranslationTable) == 'TranslationTable') {
 | 
  
    | 235 | 				$aSumTranslations = array_merge($aSumTranslations, $oTranslationTable->getArray());
 | 
  
    | 236 | 			}
 | 
  
    | 237 | 		}
 | 
  
    | 238 | 		return $aSumTranslations;
 | 
  
    | 239 | 	}
 | 
  
    | 240 | 	
 | 
  
    | 241 | } // end of class Translate
 | 
  
    | 242 | // //////////////////////////////////////////////////////////////////////////////////// //
 | 
  
    | 243 | /**
 | 
  
    | 244 |  * TranslationException
 | 
  
    | 245 |  *
 | 
  
    | 246 |  * @category     Core
 | 
  
    | 247 |  * @package      Core_Translation
 | 
  
    | 248 |  * @author       Werner v.d.Decken <wkl@isteam.de>
 | 
  
    | 249 |  * @copyright    Werner v.d.Decken <wkl@isteam.de>
 | 
  
    | 250 |  * @license      http://www.gnu.org/licenses/gpl.html   GPL License
 | 
  
    | 251 |  * @version      2.9.0
 | 
  
    | 252 |  * @revision     $Revision: 1902 $
 | 
  
    | 253 |  * @lastmodified $Date: 2013-05-12 16:19:33 +0200 (Sun, 12 May 2013) $
 | 
  
    | 254 |  * @description  Exceptionhandler for the Translation class and depending classes
 | 
  
    | 255 |  */
 | 
  
    | 256 | class TranslationException extends AppException {}
 |