Project

General

Profile

« Previous | Next » 

Revision 1864

Added by darkviper almost 12 years ago

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

View differences:

TranslationTable.php
33 33
 */
34 34
class TranslationTable {
35 35

  
36
	private $_aTranslations = array();
37
	private $_sSystemLang   = 'en';
38
	private $_sDefaultLang  = 'en';
39
	private $_sUserLang     = 'en';
40
	private $_sAddon        = '';
41
	
36
	protected $aTranslations = array();
37
	protected $aLanguages    = array();
38
	protected $sSystemLang   = 'en';
39
	protected $sDefaultLang  = 'en';
40
	protected $sUserLang     = 'en';
41
	protected $sAddon        = '';
42
	protected $oReg          = null;
43
	protected $sTempPath     = '';
44
	protected $iDirMode      = 0777;
42 45
/**
43 46
 * Constructor
44 47
 * @param string relative pathname of the Addon (i.e. '' || 'modules/myAddon/')
45
 * @param string Default language code ( 2ALPHA[[_2ALPHA]_2*4ALNUM] )
46
 * @param string User language code ( 2ALPHA[[_2ALPHA]_2*4ALNUM] )
48
 * @param string System language code ( 2*3ALPHA[[_2ALPHA]_2*4ALNUM] )
49
 * @param string Default language code ( 2*3ALPHA[[_2ALPHA]_2*4ALNUM] )
50
 * @param string User language code ( 2*3ALPHA[[_2ALPHA]_2*4ALNUM] )
47 51
 */	
48
	public function __construct($sAddon, $sDefaultLanguage, $sUserLanguage = '')
52
	public function __construct($sAddon, 
53
	                            $sSystemLanguage, $sDefaultLanguage, $sUserLanguage,
54
	                            $bUseCache = Translate::CACHE_ENABLED)
49 55
	{
50
		$this->_sDefaultLang = $sDefaultLanguage;
51
		$this->_sUserLang = ($sUserLanguage == '' ? $sDefaultLanguage : $sUserLanguage);
52
		$this->_sAddon = $sAddon;
56
		$this->bUseCache             = $bUseCache;
57
		$this->sSystemLang           = $sSystemLanguage;
58
		$this->sDefaultLang          = $sDefaultLanguage;
59
		$this->sUserLang             = $sUserLanguage;
60
		$this->sAddon                = $sAddon;
61
		if(class_exists('WbAdaptor')) {
62
			$this->sTempPath         = WbAdaptor::getInstance()->TempPath;
63
			$this->iDirMode          = WbAdaptor::getInstance()->OctalDirMode;
64
		}else {
65
			$this->sTempPath         = dirname(dirname(__FILE__)).'/temp/';
66
		}
67
		$this->aLanguages['system']  = array();
68
		$this->aLanguages['default'] = array();
69
		$this->aLanguages['user']    = array();
53 70
	}
54 71
/**
55 72
 * Load language definitions
......
58 75
 */	
59 76
	public function load($sAdaptor)
60 77
	{
61
		$c = 'TranslateAdaptor'.$sAdaptor;
62
		$oTmp = new $c($this->_sAddon);
63
		// load default language first
64
		if( ($aResult = $oTmp->loadLanguage($this->_sDefaultLang)) !== false ) {
65
			$this->_aTranslations = $aResult;
66
		}
67
		if($this->_sUserLang != $this->_sDefaultLang) {
68
		// load user language if its not equal default language
69
			if( ($aResult = $oTmp->loadLanguage($this->_sUserLang)) !== false ) {
70
				$this->_aTranslations = array_merge($this->_aTranslations, $aResult);
78
		$sCachePath	= $this->getCachePath();
79
		$sCacheFile = $sCachePath.md5($this->sAddon.$this->sSystemLang.
80
		                              $this->sDefaultLang.$this->sUserLang).'.php';
81
		if($this->bUseCache && is_readable($sCacheFile)) {
82
			$this->aTranslations = $this->loadCacheFile($sCacheFile);
83
		}else {
84
			$bLanguageFound = false;
85
			$oAdaptor= new $sAdaptor($this->sAddon);
86
			if(!$oAdaptor instanceof TranslatorAdaptorInterface) {
87
				$sMsg = 'Class ['.$sAdaptor.'] does not implement the '
88
				      . 'interface [TranslateAdaptorInterface]';
89
				throw new TranslationException($sMsg);
71 90
			}
72
		}
73
		if(($this->_sSystemLang != $this->_sUserLang) && 
74
		   ($this->_sSystemLang != $this->_sDefaultLang)) {
75
		// load system language if its not already loaded
76
			if( ($aResult = $oTmp->loadLanguage($this->_sSystemLang)) !== false ) {
77
				$this->_aTranslations = array_merge($this->_aTranslations, $aResult);
91
		// load system language first
92
			if(($aResult = $this->loadLanguageSegments($this->sSystemLang, $oAdaptor)) !== false) {
93
			// load system language
94
				$this->aLanguages['system'] = $aResult;
95
				$bLanguageFound = true;
78 96
			}
79
		}
80
		if(sizeof($this->_aTranslations) == 0) {
81
		// if absolutely no requested language found, simply get the first available 
82
			$sFirstLanguage = $oTmp->findFirstLanguage();
83
		// load first found language if its not already loaded
84
			if( ($aResult = $oTmp->loadLanguage($sFirstLanguage)) !== false ) {
85
				$this->_aTranslations = array_merge($this->_aTranslations, $aResult);
97
			if($this->sDefaultLang != $this->sSystemLang) {
98
			// load default language if it's not equal system language
99
				if(($aResult = $this->loadLanguageSegments($this->sDefaultLang, $oAdaptor)) !== false) {
100
					$this->aLanguages['default'] = $aResult;
101
					$bLanguageFound = true;
102
				}
103
			}else {
104
			// copy system language
105
				$this->aLanguages['default'] = $this->aLanguages['system'];
86 106
			}
107
			if($this->sUserLang != $this->sDefaultLang 
108
			   && $this->sUserLang != $this->sSystemLang) {
109
			// load user language if it's not equal default language or system language
110
				if(($aResult = $this->loadLanguageSegments($this->sUserLang, $oAdaptor)) !== false) {
111
					$this->aLanguages['user'] = $aResult;
112
					$bLanguageFound = true;
113
				}
114
			}elseif($this->sUserLang == $this->sDefaultLang) {
115
			// copy default language
116
				$this->aLanguages['user'] = $this->aLanguages['default'];
117
			}elseif($this->sUserLang == $this->sSystemLang) {
118
			// copy system language
119
				$this->aLanguages['user'] = $this->aLanguages['system'];
120
			}
121
			if($bLanguageFound) {
122
			// copy user into default if default is missing
123
				if(sizeof($this->aLanguages['user']) && !sizeof($this->aLanguages['default'])) {
124
					$this->aLanguages['default'] = $this->aLanguages['user'];
125
				}
126
			// copy default into system if system is missing
127
				if(sizeof($this->aLanguages['default']) && !sizeof($this->aLanguages['system'])) {
128
					$this->aLanguages['system'] = $this->aLanguages['default'];
129
				}
130
			}
131
		// if absolutely no requested language found, simply get the first available language
132
			if(!$bLanguageFound) {
133
				$sFirstLanguage = $oAdaptor->findFirstLanguage();
134
			// load first found language if its not already loaded
135
				if(($aResult = $this->loadLanguageSegments($sFirstLanguage, $oAdaptor)) !== false) {
136
					$this->aLanguages['system'] = $aResult;
137
					$bLanguageFound = true;
138
				}
139
			}
140
			if($bLanguageFound) {
141
				$this->aTranslations = array_merge($this->aTranslations,
142
				                                    $this->aLanguages['system'],
143
				                                    $this->aLanguages['default'],
144
				                                    $this->aLanguages['user']);
145
				if($this->bUseCache) {
146
					$this->writeCacheFile($sCacheFile);
147
				}
148
			}
87 149
		}
88 150
		return $this;
89 151
	}
......
94 156
 */
95 157
	public function __isset($sKey)
96 158
	{
97
		return isset($this->_aTranslations[$sKey]);
159
		return isset($this->aTranslations[$sKey]);
98 160
	}
99 161
/**
100 162
 * Get translation text
......
103 165
 */	
104 166
	public function __get($sKey)
105 167
	{
106
		if(isset($this->_aTranslations[$sKey])) {
107
			return $this->_aTranslations[$sKey];
168
		if(isset($this->aTranslations[$sKey])) {
169
			return $this->aTranslations[$sKey];
108 170
		}else {
109 171
			return '';
110 172
		}
......
116 178
 */	
117 179
	public function getArray()
118 180
	{
119
		return $this->_aTranslations;
181
		$aRetval = (is_array($this->aTranslations) ? $this->aTranslations : array());
182
		return $aRetval;
120 183
	}
184
/**
185
 * Load Language
186
 * @param string Language Code
187
 * @param object Adaptor object
188
 * @return bool|array
189
 */
190
	protected function loadLanguageSegments($sLangCode, $oAdaptor)
191
	{
192
		$aTranslations = array();
193
		// sanitize the language code
194
		$aLangCode = explode('_', preg_replace('/[^a-z0-9]/i', '_', strtolower($sLangCode)));
195
		$sConcatedLang = '';
196
		foreach($aLangCode as $sLang)
197
		{ // iterate all segments of the language code
198
			if( ($aResult = $oAdaptor->loadLanguage($sConcatedLang.$sLang)) !== false ) {
199
				$aTranslations = array_merge($aTranslations, $aResult);
200
			}
201
		}
202
		return (sizeof($aTranslations) > 0 ? $aTranslations : false);
203
	}
204
/**
205
 * getCachePath
206
 * @return string a valid path for caching or null on error
207
 */
208
	protected function getCachePath()
209
	{
210
		$sCachePath = $this->sTempPath.__CLASS__.'/cache/';
211
		if(!file_exists($sCachePath) && is_writeable($this->sTempPath)) {
212
			$iOldUmask = umask(0); 
213
			mkdir($sCachePath, $this->iDirMode, true);
214
			umask($iOldUmask); 
215
		}
216
		if(is_writable($sCachePath)) {
217
			return $sCachePath;
218
		}else {
219
			return null;
220
		}
221
	}
222
/**
223
 * load cached translation table
224
 * @param string path/name of the cachefile
225
 * @return array list of translations
226
 */	
227
	protected function loadCacheFile($sCacheFile)
228
	{
229
		$aTranslation = array();
230
		include($sCacheFile);
231
		return $aTranslation;
232
	}
233
/**
234
 * Write cache from translation
235
 * @param string path/name of the cachefile
236
 */	
237
	protected function writeCacheFile($sCacheFile)
238
	{
239
		$sOutput = '<?php'."\n".'/* autogenerated cachefile */'."\n";
240
		while (list($key, $value) = each($this->aTranslations)) {
241
			$sOutput .= '$aTranslation[\''.$key.'\'] = \''.addslashes($value).'\';'."\n";
242
		}
243
		file_put_contents($sCacheFile, $sOutput, LOCK_EX);
244
	}
121 245
} // end of class TranslationTable

Also available in: Unified diff