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: 1860 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/Translate.php $
31
 * @lastmodified $Date: 2013-02-04 10:51:05 +0100 (Mon, 04 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 $_sDefaultLanguage = 'en';
42
	protected $_sUserLanguage    = 'en';
43
	protected $_aAddons          = array();
44
	protected $_aTranslations    = array();
45

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