Project

General

Profile

1 1860 darkviper
<?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 1873 darkviper
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
26 1860 darkviper
 * @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$
30
 * @link         $HeadURL$
31
 * @lastmodified $Date$
32
 * @since        File available since 12.01.2013
33 1873 darkviper
 * @description  basic class of the Translate package
34 1860 darkviper
 */
35
class Translate {
36
37 1873 darkviper
/** holds the active singleton instance */
38 1864 darkviper
	private static $_oInstance     = null;
39 1873 darkviper
/** name of the default adaptor */
40 1864 darkviper
	protected $sAdaptor            = 'WbOldStyle';
41 1873 darkviper
/** setting for the system language (default: en) */
42 1864 darkviper
	protected $sSystemLanguage     = 'en';
43 1873 darkviper
/** setting for the default runtime language (default: en) */
44 1864 darkviper
	protected $sDefaultLanguage    = 'en';
45 1873 darkviper
/** setting for the user defined language (default: en) */
46 1864 darkviper
	protected $sUserLanguage       = 'en';
47 1873 darkviper
/** switch cache on/off */
48 1864 darkviper
	protected $bUseCache           = true;
49 1873 darkviper
/** 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 1864 darkviper
	protected $aLoadedAddons       = array();
55
/** TranslationTable object of the core and additional one activated addon */
56
	protected $aActiveTranslations = array();
57 1873 darkviper
/** possible option flags */
58
	const CACHE_DISABLED = 1; // ( 2^0 )
59
	const KEEP_MISSING   = 2; // ( 2^1 )
60 1864 darkviper
61 1860 darkviper
/** 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 1873 darkviper
 * @param string SystemLanguage code ('de' || 'de_CH' || 'de_CH_uri')
79 1860 darkviper
 * @param string DefaultLanguage code ('de' || 'de_CH' || 'de_CH_uri')
80
 * @param string UserLanguage code ('de' || 'de_CH' || 'de_CH_uri')
81 1873 darkviper
 * @param string Name of the adaptor to use
82
 * @param integer possible options (DISABLE_CACHE | KEEP_MISSING)
83 1860 darkviper
 * @throws TranslationException
84
 */
85 1873 darkviper
	public function initialize($sSystemLanguage,
86
	                           $sDefaultLanguage,
87
	                           $sUserLanguage = '',
88
	                           $sAdaptor = 'WbOldStyle',
89
	                           $iOptions = 0)
90 1860 darkviper
	{
91
		if(!class_exists('TranslateAdaptor'.$sAdaptor)) {
92
			throw new TranslationException('unable to load adaptor: '.$sAdaptor);
93
		}
94 1873 darkviper
		$this->sAdaptor       = 'TranslateAdaptor'.$sAdaptor;
95
		$this->bUseCache      = (($iOptions & self::CACHE_DISABLED) == 0);
96
		$this->bRemoveMissing = (($iOptions & self::KEEP_MISSING) == 0);
97 1864 darkviper
		// 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 1860 darkviper
		// if no user language is set then use default language
104 1864 darkviper
		$this->sUserLanguage = (trim($sUserLanguage) == ''
105 1873 darkviper
		                         ? $this->sDefaultLanguage
106 1864 darkviper
		                         : $sUserLanguage);
107 1873 darkviper
		$sPattern = '/^[a-z]{2,3}(?:(?:[_-][a-z]{2})?(?:[_-][a-z0-9]{2,4})?)$/siU';
108 1860 darkviper
		// validate language codes
109 1864 darkviper
		if(preg_match($sPattern, $this->sSystemLanguage) &&
110
		   preg_match($sPattern, $this->sDefaultLanguage) &&
111
		   preg_match($sPattern, $this->sUserLanguage))
112 1860 darkviper
		{
113
		// load core translations and activate it
114 1864 darkviper
			$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 1860 darkviper
			// 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 1864 darkviper
			$sMsg = 'Invalid language codes: ['.$this->sSystemLanguage.'] or ['
128
			      . $this->sDefaultLanguage.'] or ['.$this->sUserLanguage.']';
129 1860 darkviper
			throw new TranslationException($sMsg);
130
		}
131
	}
132
/**
133
 * Add new addon
134
 * @param string Addon descriptor (i.e. 'modules\myAddon')
135
 * @return bool
136
 */
137
	public function addAddon($sAddon)
138
	{
139 1882 darkviper
		$sAddon = str_replace('/', '\\', $sAddon);
140 1864 darkviper
		if(!(strtolower($sAddon) == 'core' || $sAddon == '' || isset($this->aLoadedAddons[$sAddon]))) {
141 1860 darkviper
		// load requested addon translations if needed and possible
142 1864 darkviper
			$oTmp = new TranslationTable($sAddon,
143
			                             $this->sSystemLanguage,
144
			                             $this->sDefaultLanguage,
145
			                             $this->sUserLanguage,
146
			                             $this->bUseCache);
147
			$this->aLoadedAddons[$sAddon] = $oTmp->load($this->sAdaptor);
148 1860 darkviper
		}
149
	}
150
/**
151
 * Activate Addon
152
 * @param string Addon descriptor (i.e. 'modules\myAddon')
153
 */
154
	public function enableAddon($sAddon)
155
	{
156 1882 darkviper
		$sAddon = str_replace('/', '\\', $sAddon);
157 1860 darkviper
		if(!(strtolower($sAddon) == 'core' || $sAddon == '')) {
158 1864 darkviper
			if(!isset($this->aLoadedAddons[$sAddon])) {
159 1860 darkviper
				$this->addAddon($sAddon);
160
			}
161 1864 darkviper
			$this->aActiveTranslations[1] = $this->aLoadedAddons[$sAddon];
162 1860 darkviper
		}
163
164
	}
165 1864 darkviper
/**
166
 * Dissable active addon
167
 */
168 1860 darkviper
	public function disableAddon()
169
	{
170 1864 darkviper
		if(isset($this->aActiveTranslations[1])) {
171
			unset($this->aActiveTranslations[1]);
172 1860 darkviper
		}
173
	}
174
175
/**
176
 * Is key available
177
 * @param string Language key
178
 * @return bool
179
 */
180
	public function __isset($sKey)
181
	{
182 1864 darkviper
		foreach($this->aActiveTranslations as $oAddon) {
183 1860 darkviper
			if(isset($oAddon->$sKey)) {
184
			// is true if at least one translation is found
185
				return true;
186
			}
187
		}
188
		return false;
189
	}
190
/**
191
 * Get translation text
192
 * @param string Language key
193
 * @return string Translation text
194
 */
195
	public function __get($sKey)
196
	{
197 1873 darkviper
		$sRetval = ($this->bRemoveMissing ? '' : '#'.$sKey.'#');
198 1864 darkviper
		foreach($this->aActiveTranslations as $oAddon) {
199 1860 darkviper
			if(isset($oAddon->$sKey)) {
200
			// search the last matching translation (Core -> Addon)
201
				$sRetval = $oAddon->$sKey;
202
			}
203
		}
204
		return $sRetval;
205
	}
206
/**
207 1882 darkviper
 * Protect class from property injections
208
 * @param string name of property
209
 * @param mixed value
210
 * @throws TranslationException
211
 */
212
	public function __set($name, $value) {
213
		throw new TranslationException('tried to set a readonly or nonexisting property ['.$name.']!! ');
214
	}
215
/**
216 1860 darkviper
 * Return complete table of translations
217
 * @return array
218
 * @deprecated for backward compatibility only. Will be removed shortly
219
 */
220
	public function getLangArray()
221
	{
222 1864 darkviper
		$aSumTranslations = array();
223
		foreach($this->aActiveTranslations as $oTranslationTable) {
224
			if(get_class($oTranslationTable) == 'TranslationTable') {
225
				$aSumTranslations = array_merge($aSumTranslations, $oTranslationTable->getArray());
226
			}
227 1860 darkviper
		}
228 1864 darkviper
		return $aSumTranslations;
229 1860 darkviper
	}
230
231
} // end of class Translate
232
// //////////////////////////////////////////////////////////////////////////////////// //
233
/**
234
 * TranslationException
235
 *
236
 * @category     Core
237
 * @package      Core_Translation
238
 * @author       Werner v.d.Decken <wkl@isteam.de>
239
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
240
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
241
 * @version      2.9.0
242
 * @revision     $Revision$
243
 * @lastmodified $Date$
244
 * @description  Exceptionhandler for the Translation class and depending classes
245
 */
246
class TranslationException extends AppException {}