Project

General

Profile

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
 * @author       Werner v.d.Decken <wkl@isteam.de>
26
 * @copyright    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: 1864 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/Translate.php $
31
 * @lastmodified $Date: 2013-02-19 12:40:12 +0100 (Tue, 19 Feb 2013) $
32
 * @since        File available since 12.01.2013
33
 * @description
34
 */
35
class Translate {
36
	
37
//  @object hold the Singleton instance 
38
	private static $_oInstance     = null;
39
	
40
	protected $sAdaptor            = 'WbOldStyle';
41
	protected $sSystemLanguage     = 'en';
42
	protected $sDefaultLanguage    = 'en';
43
	protected $sUserLanguage       = 'en';
44
	protected $bUseCache           = true;
45
/** TranslationTable objects of all loaded addons */
46
	protected $aLoadedAddons       = array();
47
/** TranslationTable object of the core and additional one activated addon */	
48
	protected $aActiveTranslations = array();
49
	
50
	const CACHE_ENABLED = true;
51
	const CACHE_DISABLED = false;
52

    
53
/** prevent class from public instancing and get an object to hold extensions */
54
	protected function  __construct() {}
55
/** prevent from cloning existing instance */
56
	private function __clone() {}
57
/**
58
 * get a valid instance of this class
59
 * @return object
60
 */
61
	static public function getInstance() {
62
		if( is_null(self::$_oInstance) ) {
63
            $c = __CLASS__;
64
            self::$_oInstance = new $c;
65
		}
66
		return self::$_oInstance;
67
	}
68
/**
69
 * Initialize the Translations
70
 * @param string DefaultLanguage code ('de' || 'de_CH' || 'de_CH_uri')
71
 * @param string UserLanguage code ('de' || 'de_CH' || 'de_CH_uri')
72
 * @throws TranslationException
73
 */
74
	public function initialize($sSystemLanguage, $sDefaultLanguage, $sUserLanguage = '', 
75
	                           $sAdaptor = 'WbOldStyle', $bUseCache = self::CACHE_ENABLED) 
76
	{
77
		if(!class_exists('TranslateAdaptor'.$sAdaptor)) {
78
			throw new TranslationException('unable to load adaptor: '.$sAdaptor);
79
		}
80
		$this->bUseCache = $bUseCache;
81
		$this->sAdaptor = 'TranslateAdaptor'.$sAdaptor;
82
		// if no system language is set then use language 'en'
83
		$this->sSystemLanguage = (trim($sSystemLanguage) == '' ? 'en' : $sSystemLanguage);
84
		// if no default language is set then use system language
85
		$this->sDefaultLanguage = (trim($sDefaultLanguage) == '' 
86
		                            ? $this->sSystemLanguage 
87
		                            : $sDefaultLanguage);
88
		// if no user language is set then use default language
89
		$this->sUserLanguage = (trim($sUserLanguage) == '' 
90
		                         ? $this->sDefaultLanguage 
91
		                         : $sUserLanguage);
92
		$sPattern = '/^[a-z]{2,3}(?:(?:\_[a-z]{2})?(?:\_[a-z0-9]{2,4})?)$/siU';
93
		// validate language codes
94
		if(preg_match($sPattern, $this->sSystemLanguage) &&
95
		   preg_match($sPattern, $this->sDefaultLanguage) &&
96
		   preg_match($sPattern, $this->sUserLanguage))
97
		{
98
		// load core translations and activate it
99
			$oTmp = new TranslationTable('', 
100
			                             $this->sSystemLanguage, 
101
			                             $this->sDefaultLanguage, 
102
			                             $this->sUserLanguage,
103
			                             $this->bUseCache);
104
			$this->aLoadedAddons['core'] = $oTmp->load($this->sAdaptor);
105
			$this->aActiveTranslations[0] = $this->aLoadedAddons['core'];
106
			if(sizeof($this->aLoadedAddons['core']) == 0) {
107
			// throw an exception for missing translations
108
				throw new TranslationException('missing core translations');
109
			}
110
		}else {
111
		// throw an exception for invalid or missing language codes
112
			$sMsg = 'Invalid language codes: ['.$this->sSystemLanguage.'] or ['
113
			      . $this->sDefaultLanguage.'] or ['.$this->sUserLanguage.']';
114
			throw new TranslationException($sMsg);
115
		}
116
	}
117
/**
118
 * Add new addon
119
 * @param string Addon descriptor (i.e. 'modules\myAddon')
120
 * @return bool 
121
 */	
122
	public function addAddon($sAddon)
123
	{
124
		if(!(strtolower($sAddon) == 'core' || $sAddon == '' || isset($this->aLoadedAddons[$sAddon]))) {
125
		// load requested addon translations if needed and possible
126
			$oTmp = new TranslationTable($sAddon, 
127
			                             $this->sSystemLanguage, 
128
			                             $this->sDefaultLanguage, 
129
			                             $this->sUserLanguage,
130
			                             $this->bUseCache);
131
			$this->aLoadedAddons[$sAddon] = $oTmp->load($this->sAdaptor);
132
		}
133
	}
134
/**
135
 * Activate Addon
136
 * @param string Addon descriptor (i.e. 'modules\myAddon')
137
 */	
138
	public function enableAddon($sAddon)
139
	{
140
		if(!(strtolower($sAddon) == 'core' || $sAddon == '')) {
141
			if(!isset($this->aLoadedAddons[$sAddon])) {
142
				$this->addAddon($sAddon);
143
			}
144
			$this->aActiveTranslations[1] = $this->aLoadedAddons[$sAddon];
145
		}
146
		
147
	}
148
/**
149
 * Dissable active addon
150
 */	
151
	public function disableAddon()
152
	{
153
		if(isset($this->aActiveTranslations[1])) {
154
			unset($this->aActiveTranslations[1]);
155
		}
156
	}
157
	
158
/**
159
 * Is key available
160
 * @param string Language key
161
 * @return bool
162
 */
163
	public function __isset($sKey)
164
	{
165
		foreach($this->aActiveTranslations as $oAddon) {
166
			if(isset($oAddon->$sKey)) {
167
			// is true if at least one translation is found
168
				return true;
169
			}
170
		}
171
		return false;
172
	}
173
/**
174
 * Get translation text
175
 * @param string Language key
176
 * @return string Translation text
177
 */	
178
	public function __get($sKey)
179
	{
180
		$sRetval = '';
181
		foreach($this->aActiveTranslations as $oAddon) {
182
			if(isset($oAddon->$sKey)) {
183
			// search the last matching translation (Core -> Addon)
184
				$sRetval = $oAddon->$sKey;
185
			}
186
		}
187
		return $sRetval;
188
	}
189
/**
190
 * Return complete table of translations
191
 * @return array
192
 * @deprecated for backward compatibility only. Will be removed shortly
193
 */	
194
	public function getLangArray()
195
	{
196
		$aSumTranslations = array();
197
		foreach($this->aActiveTranslations as $oTranslationTable) {
198
			if(get_class($oTranslationTable) == 'TranslationTable') {
199
				$aSumTranslations = array_merge($aSumTranslations, $oTranslationTable->getArray());
200
			}
201
		}
202
		return $aSumTranslations;
203
	}
204
	
205
} // end of class Translate
206
// //////////////////////////////////////////////////////////////////////////////////// //
207
/**
208
 * TranslationException
209
 *
210
 * @category     Core
211
 * @package      Core_Translation
212
 * @author       Werner v.d.Decken <wkl@isteam.de>
213
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
214
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
215
 * @version      2.9.0
216
 * @revision     $Revision: 1864 $
217
 * @lastmodified $Date: 2013-02-19 12:40:12 +0100 (Tue, 19 Feb 2013) $
218
 * @description  Exceptionhandler for the Translation class and depending classes
219
 */
220
class TranslationException extends AppException {}
(7-7/30)