Project

General

Profile

« Previous | Next » 

Revision 1860

Added by darkviper over 11 years ago

added new translation classes (Translate/TranslationTable/TranslateAdaptorWbOldStyle) for easy handling of language files
initialize and activate class Translate in /framework/initialize.php

View differences:

branches/2.8.x/CHANGELOG
11 11
! = Update/Change
12 12
===============================================================================
13 13

  
14

  
14
04 Feb-2013 Build 1860 Werner v.d.Decken(DarkViper)
15
+ added new translation classes (Translate/TranslationTable/TranslateAdaptorWbOldStyle) for easy handling of language files
16
! initialize and activate class Translate in /framework/initialize.php
15 17
11 Jan-2013 Build 1859 Dietmar Woellbrink (Luisehahne)
16 18
# bugfix  mkdir(): Invalid argument if create acessfile
17 19
11 Jan-2013 Build 1858 Dietmar Woellbrink (Luisehahne)
branches/2.8.x/wb/admin/interface/version.php
51 51

  
52 52
// check if defined to avoid errors during installation (redirect to admin panel fails if PHP error/warnings are enabled)
53 53
if(!defined('VERSION')) define('VERSION', '2.8.3');
54
if(!defined('REVISION')) define('REVISION', '1859');
54
if(!defined('REVISION')) define('REVISION', '1860');
55 55
if(!defined('SP')) define('SP', '');
branches/2.8.x/wb/framework/Translate.php
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$
30
 * @link         $HeadURL$
31
 * @lastmodified $Date$
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$
187
 * @lastmodified $Date$
188
 * @description  Exceptionhandler for the Translation class and depending classes
189
 */
190
class TranslationException extends AppException {}
0 191

  
branches/2.8.x/wb/framework/TranslationTable.php
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
 * TranslationTable.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$
30
 * @link         $HeadURL$
31
 * @lastmodified $Date$
32
 * @since        File available since 12.01.2013
33
 */
34
class TranslationTable {
35

  
36
	private $_aTranslations = array();
37
	private $_sSystemLang   = 'en';
38
	private $_sDefaultLang  = 'en';
39
	private $_sUserLang     = 'en';
40
	private $_sAddon        = '';
41
	
42
/**
43
 * Constructor
44
 * @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] )
47
 */	
48
	public function __construct($sAddon, $sDefaultLanguage, $sUserLanguage = '')
49
	{
50
		$this->_sDefaultLang = $sDefaultLanguage;
51
		$this->_sUserLang = ($sUserLanguage == '' ? $sDefaultLanguage : $sUserLanguage);
52
		$this->_sAddon = $sAddon;
53
	}
54
/**
55
 * Load language definitions
56
 * @return TranslationTable a valid translation table object
57
 * @throws TranslationException
58
 */	
59
	public function load($sAdaptor)
60
	{
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);
71
			}
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);
78
			}
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);
86
			}
87
		}
88
		return $this;
89
	}
90
/**
91
 * Is key available
92
 * @param string Language key
93
 * @return bool
94
 */
95
	public function __isset($sKey)
96
	{
97
		return isset($this->_aTranslations[$sKey]);
98
	}
99
/**
100
 * Get translation text
101
 * @param string Language key
102
 * @return string Translation text
103
 */	
104
	public function __get($sKey)
105
	{
106
		if(isset($this->_aTranslations[$sKey])) {
107
			return $this->_aTranslations[$sKey];
108
		}else {
109
			return '';
110
		}
111
	}
112
/**
113
 * returns the whoole translation array
114
 * @return array
115
 * @deprecated for backward compatibility only. Will be removed shortly
116
 */	
117
	public function getArray()
118
	{
119
		return $this->_aTranslations;
120
	}
121
} // end of class TranslationTable
0 122

  
branches/2.8.x/wb/framework/TranslateAdaptorWbOldStyle.php
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
 * TranslationTable.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$
30
 * @link         $HeadURL$
31
 * @lastmodified $Date$
32
 * @since        File available since 12.01.2013
33
 * @description  Loads translation table from old languagefiles before WB-2.9.0.
34
 *               Can handle Languagecodes like 'de_DE_BAY' (2ALPHA_2ALPHA_2-4ALNUM)
35
 */
36
class TranslateAdaptorWbOldStyle {
37

  
38
	protected $sAppPath   = '';
39
	protected $sAddon     = '';
40
	protected $aLangTable = array();
41
	protected $sFilePath  = '';
42
/**
43
 * Constructor
44
 * @param string descriptor of the Addon (i.e. '' || 'modules\myAddon'
45
 */
46
	public function __construct($sAddon = '')
47
	{
48
		$this->sAddon = $sAddon;
49
		$this->sAppPath = dirname(dirname(__FILE__)).'/';
50
		$this->sFilePath = $this->sAppPath
51
		                 . trim(str_replace('\\', '/', $sAddon), '/').'/languages/';
52
	}
53
/**
54
 * Load languagefile
55
 * @param string $sLangCode
56
 * @return array|bool an array of translations or FALSE on error
57
 */
58
	public function loadLanguage($sLangCode)
59
	{
60
		$aTranslations = array();
61
		// sanitize the language code
62
		$aLangCode = explode('_', preg_replace('/[^a-z0-9]/i', '_', strtolower($sLangCode)));
63
		$sConcatedLang = '';
64
		foreach($aLangCode as $sLang)
65
		{ // iterate all segments of the language code
66
			$sLangFile = '';
67
			$sLang = strtolower($sLang);
68
			// seek lowerchars file
69
			if(is_readable($this->sFilePath.$sConcatedLang.$sLang.'.php')) {
70
				$sLangFile = $this->sFilePath.$sConcatedLang.$sLang.'.php';
71
			}else {
72
				// seek upperchars file
73
				$sLang = strtoupper($sLang);
74
				if(is_readable($this->sFilePath.$sConcatedLang.$sLang.'.php')) {
75
					$sLangFile = $this->sFilePath.$sConcatedLang.$sLang.'.php';
76
				}
77
			}
78
			if($sLangFile) {
79
			// import the fond file
80
				$sConcatedLang .= $sLangFile.'_';
81
				$aTmp = $this->_importArrays($sLangFile);
82
				$aTranslations = array_merge($aTranslations, $aTmp);
83
			}
84
		}
85
		return (sizeof($aTranslations) > 0 ? $aTranslations : false);
86
	}
87
/**
88
 * Find first existing language
89
 * @return string Code of first found language
90
 */
91
	public function findFirstLanguage()
92
	{
93
	// search for first available and readable language file
94
		$sRetval = '';
95
		if(is_readable($this->sFilePath)) {
96
			$iterator = new DirectoryIterator($this->sFilePath);
97
			foreach ($iterator as $oFileInfo) {
98
				$sPattern = '/^[a-z0-9]{2}(?:(?:\_[a-z0-9]{2})?(?:\_[a-z0-9]{2,4})?)\.php/siU';
99
				if(!preg_match($sPattern, $oFileInfo->getBasename())) { continue; }
100
				if($oFileInfo->isReadable()) {
101
					$sRetval = $oFileInfo->getBasename('.php');
102
					break;
103
				}
104
			}
105
		}
106
		return $sRetval;
107
	}
108
/**
109
 * Import language definitions into array
110
 * @param string load language from filename
111
 * @return array contains all found translations
112
 */
113
	private function _importArrays($sLanguageFile)
114
	{
115
		// include the file
116
		include($sLanguageFile);
117
		// get all available loaded vars of this method
118
		$aAllVars = get_defined_vars();
119
		$aLangSections = array();
120
		$aLanguageTable = array();
121
		foreach($aAllVars as $key=>$value) {
122
		// extract the names of arrays from language file
123
			if(is_array($value)) {
124
				$aLangSections[] = $key;
125
			}
126
		}
127
		foreach($aLangSections as $sSection) {
128
		// walk through all arrays
129
			foreach(${$sSection} as $key => $value) {
130
			// and import all found translations
131
				$aLanguageTable[$sSection.'_'.$key] = $value;
132
			}
133
		}
134
		return $aLanguageTable;
135
	}
136
} // end of class TranslateAdaptorWbOldStyle
0 137

  

Also available in: Unified diff