1
|
<?php
|
2
|
/**
|
3
|
* @category WebsiteBaker
|
4
|
* @package WebsiteBaker_Core
|
5
|
* @author Werner v.d.Decken
|
6
|
* @copyright WebsiteBaker Org e.V.
|
7
|
* @license http://www.gnu.org/licenses/gpl.html
|
8
|
* @version $Id: ModLanguage.php 1866 2013-02-19 20:47:39Z Luisehahne $
|
9
|
* @filesource $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/ModLanguage.php $
|
10
|
* @since Datei vorhanden seit Release 2.8.4
|
11
|
* @lastmodified $Date: 2013-02-19 21:47:39 +0100 (Tue, 19 Feb 2013) $
|
12
|
* @deprecated 2013/02/19
|
13
|
*/
|
14
|
class ModLanguage {
|
15
|
|
16
|
// @var string 2 upercase chars for hardcoded system default language
|
17
|
private $_sSystemLanguage = 'EN';
|
18
|
// @var string 2 upercase chars for default fallback language
|
19
|
private $_sDefaultLanguage = '';
|
20
|
// @var string 2 upercase chars for current active language
|
21
|
private $_sCurrentLanguage = '';
|
22
|
// @var string full directory with trailing slash to search language files in
|
23
|
private $_sLanguageDirectory = '';
|
24
|
// @array list to hold the complete resulting translation table
|
25
|
private $_aLanguageTable = array();
|
26
|
// @array list of all loaded/merged languages
|
27
|
private $_aLoadedLanguages = array();
|
28
|
|
29
|
// @boolean set to TRUE if language is successfully loaded
|
30
|
private $_bLoaded = false;
|
31
|
// @object hold the Singleton instance
|
32
|
private static $_oInstance = null;
|
33
|
|
34
|
/**
|
35
|
* prevent class from public instancing
|
36
|
*/
|
37
|
final protected function __construct() { }
|
38
|
/**
|
39
|
* prevent from cloning existing instance
|
40
|
*/
|
41
|
final private function __clone() {}
|
42
|
/**
|
43
|
* get a valid instance of this class
|
44
|
* @return object
|
45
|
*/
|
46
|
static public function getInstance() {
|
47
|
if( is_null(self::$_oInstance) ) {
|
48
|
$c = __CLASS__;
|
49
|
self::$_oInstance = new $c;
|
50
|
}
|
51
|
return self::$_oInstance;
|
52
|
}
|
53
|
/**
|
54
|
* return requested translation for a key
|
55
|
* @param string $sLanguageKey 2-uppercase letters language code
|
56
|
* @return string found translation or empty string
|
57
|
* @throws TranslationException
|
58
|
*/
|
59
|
public function __get($sLanguageKey)
|
60
|
{
|
61
|
if($this->_bLoaded) {
|
62
|
$sRetval = (isset($this->_aLanguageTable[$sLanguageKey])
|
63
|
? $this->_aLanguageTable[$sLanguageKey] : '{missing: '.$sLanguageKey.'}');
|
64
|
return $sRetval;
|
65
|
}
|
66
|
$msg = 'No translation table loaded';
|
67
|
throw new TranslationException($msg);
|
68
|
}
|
69
|
/**
|
70
|
* returns the whoole language array for use in templateengine
|
71
|
* @return array
|
72
|
* @throws TranslationException
|
73
|
*/
|
74
|
public function getLangArray()
|
75
|
{
|
76
|
if($this->_bLoaded) {
|
77
|
return $this->_aLanguageTable;
|
78
|
}
|
79
|
$msg = 'No translation table loaded';
|
80
|
throw new TranslationException($msg);
|
81
|
}
|
82
|
/**
|
83
|
* set language and load needed language file
|
84
|
* @param string $sDirectory full path to the language files
|
85
|
* @param string $sLanguage 2 chars current active language code
|
86
|
* @param string $sDefault 2 chars default fallback language code
|
87
|
* @throws SecDirectoryTraversalException [global exception]
|
88
|
* @throws TranslationException [private exception]
|
89
|
*/
|
90
|
public function setLanguage($sDirectory, $sCurrentLanguage, $sDefaultLanguage = 'EN')
|
91
|
{
|
92
|
// sanitize arguments
|
93
|
$sBasePath = realpath(dirname(dirname(__FILE__)));
|
94
|
$sLangDir = realpath($sDirectory);
|
95
|
if(preg_match('/^'.preg_quote($sBasePath, '/').'/', $sLangDir)) {
|
96
|
$sLangDir = rtrim(str_replace('\\', '/', $sLangDir), '/').'/';
|
97
|
$sCurrentLanguage = strtoupper($sCurrentLanguage);
|
98
|
$sDefaultLanguage = strtoupper($sDefaultLanguage);
|
99
|
// check if the requested language is not already loaded
|
100
|
if($this->_sLanguageDirectory != $sLangDir ||
|
101
|
$this->_sCurrentLanguage != $sCurrentLanguage ||
|
102
|
$this->_sDefaultLanguage != $sDefaultLanguage)
|
103
|
{
|
104
|
// now load and merge the files in order SYSTEM - DEFAULT - CURRENT
|
105
|
$this->_aLanguageTable = array();
|
106
|
// at first load DEFAULT_LANGUAGE
|
107
|
$this->_loadLanguage($sLangDir, $sDefaultLanguage);
|
108
|
// at second merge CURRENT_LANGUAGE to front if not already loaded
|
109
|
if(!in_array($sCurrentLanguage, $this->_aLoadedLanguages)) {
|
110
|
$this->_loadLanguage($sLangDir, $sCurrentLanguage);
|
111
|
}
|
112
|
// at last merge SYSTEM_LANGUAGE to background if not already loaded
|
113
|
if(!in_array($this->_sSystemLanguage, $this->_aLoadedLanguages)) {
|
114
|
$this->_loadLanguage($sLangDir, $this->_sSystemLanguage, true);
|
115
|
}
|
116
|
// if no predefined language was fond, search for first available language
|
117
|
if(sizeof($this->_aLanguageTable) == 0) {
|
118
|
// if absolutely no language was fond, throw an exception
|
119
|
if(false !== ($sRandomLanguage = $this->_findFirstLanguage($sLangDir))) {
|
120
|
$this->_loadLanguage($sLangDir, $sRandomLanguage);
|
121
|
}
|
122
|
}
|
123
|
// remember last settings
|
124
|
$this->_sLanguageDirectory = $sLangDir;
|
125
|
$this->_sCurrentLanguage = $sCurrentLanguage;
|
126
|
$this->_sDefaultLanguage = $sDefaultLanguage;
|
127
|
}
|
128
|
if(!($this->_bLoaded = (sizeof($this->_aLanguageTable) != 0))) {
|
129
|
$msg = 'unable to find valid language definition file in<br />';
|
130
|
$msg .= '"'.str_replace($sBasePath, '', $this->_sLanguageDirectory).'"';
|
131
|
throw new TranslationException($msg);
|
132
|
}
|
133
|
$this->_bLoaded = true;
|
134
|
}else {
|
135
|
throw new SecDirectoryTraversalException($sLangDir);
|
136
|
}
|
137
|
}
|
138
|
/**
|
139
|
* load language from given directory
|
140
|
* @param string $sLangDir
|
141
|
* @param string $sLanguage
|
142
|
*/
|
143
|
private function _loadLanguage($sLangDir, $sLanguage, $bLoadSystemLanguage = false)
|
144
|
{
|
145
|
if(is_readable($sLangDir.$sLanguage.'.php')) {
|
146
|
$aTemp = $this->_importArrays($sLangDir.$sLanguage.'.php');
|
147
|
if($bLoadSystemLanguage) {
|
148
|
$this->_aLanguageTable = array_merge($aTemp, $this->_aLanguageTable);
|
149
|
}else {
|
150
|
$this->_aLanguageTable = array_merge($this->_aLanguageTable, $aTemp);
|
151
|
}
|
152
|
$this->_aLoadedLanguages[] = $sLanguage;
|
153
|
}
|
154
|
}
|
155
|
/**
|
156
|
* find first available language in given directory
|
157
|
* @param string $sLangDir the directory to scan for language files
|
158
|
* @return string returns the 2 char language code or FALSE if search fails
|
159
|
*/
|
160
|
private function _findFirstLanguage($sLangDir)
|
161
|
{
|
162
|
// search for first available and readable language file
|
163
|
$sRetval = false;
|
164
|
if(is_readable($sLangDir)) {
|
165
|
$iterator = new DirectoryIterator($sLangDir);
|
166
|
foreach ($iterator as $fileinfo) {
|
167
|
if(!preg_match('/^[A-Z]{2}\.php$/', $fileinfo->getBasename())) { continue; }
|
168
|
$sLanguageFile = $fileinfo->getPathname();
|
169
|
if(is_readable($sLanguageFile)) {
|
170
|
$sRetval = basename($sLanguageFile, '.php');
|
171
|
break;
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
return $sRetval;
|
176
|
}
|
177
|
/**
|
178
|
* import key-values from language file
|
179
|
* @param string $sLanguageFile
|
180
|
* @return array of language definitions
|
181
|
*/
|
182
|
private function _importArrays($sLanguageFile)
|
183
|
{
|
184
|
include($sLanguageFile);
|
185
|
$aAllVars = get_defined_vars();
|
186
|
$aLangSections = array();
|
187
|
$aLanguageTable = array();
|
188
|
foreach($aAllVars as $key=>$value) {
|
189
|
// extract the names of arrays from language file
|
190
|
if(is_array($value)) {
|
191
|
$aLangSections[] = $key;
|
192
|
}
|
193
|
}
|
194
|
foreach($aLangSections as $sSection) {
|
195
|
// walk through all arrays
|
196
|
foreach(${$sSection} as $key => $value) {
|
197
|
// and import all found translations
|
198
|
$aLanguageTable[$sSection.'_'.$key] = $value;
|
199
|
}
|
200
|
}
|
201
|
return $aLanguageTable;
|
202
|
}
|
203
|
} // end class Translate
|
204
|
/**
|
205
|
* Exception class for Translation
|
206
|
*/
|
207
|
//class TranslationException extends AppException {}
|