Project

General

Profile

« Previous | Next » 

Revision 1864

Added by darkviper over 11 years ago

update classes Translate, TranslateTable, TranslateAdaptorWbOldStyle
added interface TranslateAdaptorInterface
added Klasse_Translate_de.pdf
update class WbAdaptor
update /framework/initialize.php

View differences:

Translate.php
35 35
class Translate {
36 36
	
37 37
//  @object hold the Singleton instance 
38
	private static $_oInstance   = null;
38
	private static $_oInstance     = null;
39 39
	
40
	protected $_sAdaptor         = 'WbOldStyle';
41
	protected $_sDefaultLanguage = 'en';
42
	protected $_sUserLanguage    = 'en';
43
	protected $_aAddons          = array();
44
	protected $_aTranslations    = array();
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;
45 52

  
46 53
/** prevent class from public instancing and get an object to hold extensions */
47 54
	protected function  __construct() {}
......
64 71
 * @param string UserLanguage code ('de' || 'de_CH' || 'de_CH_uri')
65 72
 * @throws TranslationException
66 73
 */
67
	public function initialize($sDefaultLanguage, $sUserLanguage = '', $sAdaptor = 'WbOldStyle') 
74
	public function initialize($sSystemLanguage, $sDefaultLanguage, $sUserLanguage = '', 
75
	                           $sAdaptor = 'WbOldStyle', $bUseCache = self::CACHE_ENABLED) 
68 76
	{
69 77
		if(!class_exists('TranslateAdaptor'.$sAdaptor)) {
70 78
			throw new TranslationException('unable to load adaptor: '.$sAdaptor);
71 79
		}
72
		$this->_sAdaptor = $sAdaptor;
73
		$this->_sDefaultLanguage = $sDefaultLanguage;
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);
74 88
		// if no user language is set then use default language
75
		$this->_sUserLanguage = ($sUserLanguage == '' ? $sDefaultLanguage : $sUserLanguage);
89
		$this->sUserLanguage = (trim($sUserLanguage) == '' 
90
		                         ? $this->sDefaultLanguage 
91
		                         : $sUserLanguage);
76 92
		$sPattern = '/^[a-z]{2,3}(?:(?:\_[a-z]{2})?(?:\_[a-z0-9]{2,4})?)$/siU';
77 93
		// validate language codes
78
		if(preg_match($sPattern, $this->_sDefaultLanguage) &&
79
		   preg_match($sPattern, $this->_sUserLanguage))
94
		if(preg_match($sPattern, $this->sSystemLanguage) &&
95
		   preg_match($sPattern, $this->sDefaultLanguage) &&
96
		   preg_match($sPattern, $this->sUserLanguage))
80 97
		{
81 98
		// 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) {
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) {
86 107
			// throw an exception for missing translations
87 108
				throw new TranslationException('missing core translations');
88 109
			}
89 110
		}else {
90 111
		// throw an exception for invalid or missing language codes
91
			$sMsg = 'Invalid language codes: ['.$this->_sDefaultLanguage.'] ['.$this->_sUserLanguage.']';
112
			$sMsg = 'Invalid language codes: ['.$this->sSystemLanguage.'] or ['
113
			      . $this->sDefaultLanguage.'] or ['.$this->sUserLanguage.']';
92 114
			throw new TranslationException($sMsg);
93 115
		}
94 116
	}
......
99 121
 */	
100 122
	public function addAddon($sAddon)
101 123
	{
102
		if(!(strtolower($sAddon) == 'core' || $sAddon == '' || isset($this->_aAddons[$sAddon]))) {
124
		if(!(strtolower($sAddon) == 'core' || $sAddon == '' || isset($this->aLoadedAddons[$sAddon]))) {
103 125
		// 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);
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);
106 132
		}
107 133
	}
108 134
/**
......
112 138
	public function enableAddon($sAddon)
113 139
	{
114 140
		if(!(strtolower($sAddon) == 'core' || $sAddon == '')) {
115
			if(!isset($this->_aAddons[$sAddon])) {
141
			if(!isset($this->aLoadedAddons[$sAddon])) {
116 142
				$this->addAddon($sAddon);
117 143
			}
118
			$this->_aTranslations[1] = $this->_aAddons[$sAddon];
144
			$this->aActiveTranslations[1] = $this->aLoadedAddons[$sAddon];
119 145
		}
120 146
		
121 147
	}
122
	
148
/**
149
 * Dissable active addon
150
 */	
123 151
	public function disableAddon()
124 152
	{
125
		if(isset($this->_aTranslations[1])) {
126
			unset($this->_aTranslations[1]);
153
		if(isset($this->aActiveTranslations[1])) {
154
			unset($this->aActiveTranslations[1]);
127 155
		}
128 156
	}
129 157
	
......
134 162
 */
135 163
	public function __isset($sKey)
136 164
	{
137
		foreach($this->_aTranslations as $oAddon) {
165
		foreach($this->aActiveTranslations as $oAddon) {
138 166
			if(isset($oAddon->$sKey)) {
139 167
			// is true if at least one translation is found
140 168
				return true;
......
150 178
	public function __get($sKey)
151 179
	{
152 180
		$sRetval = '';
153
		foreach($this->_aTranslations as $oAddon) {
181
		foreach($this->aActiveTranslations as $oAddon) {
154 182
			if(isset($oAddon->$sKey)) {
155 183
			// search the last matching translation (Core -> Addon)
156 184
				$sRetval = $oAddon->$sKey;
......
165 193
 */	
166 194
	public function getLangArray()
167 195
	{
168
		$aTranslations = array();
169
		foreach($this->_aTranslations as $aTranslation) {
170
			$aTranslations = array_merge($aTranslations, $aTranslation);
196
		$aSumTranslations = array();
197
		foreach($this->aActiveTranslations as $oTranslationTable) {
198
			if(get_class($oTranslationTable) == 'TranslationTable') {
199
				$aSumTranslations = array_merge($aSumTranslations, $oTranslationTable->getArray());
200
			}
171 201
		}
172
		return $aTranslations;
202
		return $aSumTranslations;
173 203
	}
174 204
	
175 205
} // end of class Translate

Also available in: Unified diff