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 1683 2012-05-03 14:43:27Z darkviper $
|
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.2
|
11
|
* @lastmodified $Date: 2012-05-03 16:43:27 +0200 (Thu, 03 May 2012) $
|
12
|
*/
|
13
|
class ModLanguage {
|
14
|
|
15
|
private $_sCurrentLanguage = '';
|
16
|
private $_sDefaultLanguage = '';
|
17
|
private $_sLanguageDirectory = '';
|
18
|
private $_sLanguageFile = '';
|
19
|
private $_LanguageTable = array();
|
20
|
private $_bLoaded = false;
|
21
|
|
22
|
private static $_oInstance = null;
|
23
|
/* prevent from public instancing */
|
24
|
protected function __construct() { }
|
25
|
/* prevent from cloning */
|
26
|
private function __clone() {}
|
27
|
/**
|
28
|
* get a valid instance of this class
|
29
|
* @return object
|
30
|
*/
|
31
|
static public function getInstance() {
|
32
|
if( is_null(self::$_oInstance) ) {
|
33
|
$c = __CLASS__;
|
34
|
self::$_oInstance = new $c;
|
35
|
}
|
36
|
return self::$_oInstance;
|
37
|
}
|
38
|
/**
|
39
|
* set language and load needed language file
|
40
|
* @param string $sDirectory full path to the language files
|
41
|
* @param string $sLanguage 2-letters language code
|
42
|
* @param string $sDefault 2-letters default-language code
|
43
|
*/
|
44
|
public function setLanguage($sDirectory, $sLanguage, $sDefault = 'EN')
|
45
|
{
|
46
|
$sBasePath = realpath(dirname(dirname(__FILE__)));
|
47
|
$sLangDir = realpath($sDirectory);
|
48
|
if(!preg_match('/^'.preg_quote($sBasePath, '/').'/', $sLangDir)) {
|
49
|
throw new SecDirectoryTraversalException();
|
50
|
}
|
51
|
$sLangDir = str_replace('\\', '/', $sLangDir);
|
52
|
$sLangDir = rtrim($sLangDir, '/').'/';
|
53
|
$sLanguage = strtoupper($sLanguage);
|
54
|
$sLanguage = strtoupper($sDefault);
|
55
|
if($this->_sLanguageDirectory != $sLangDir ||
|
56
|
$this->_sCurrentLanguage != $sLanguage ||
|
57
|
$this->_sDefaultLanguage != $sDefault)
|
58
|
{
|
59
|
// only load language if not already loaded
|
60
|
$this->_sLanguageDirectory = rtrim($sLangDir, '/').'/';
|
61
|
$this->_sCurrentLanguage = $sLanguage;
|
62
|
$this->_sDefaultLanguage = $sDefault;
|
63
|
|
64
|
if(!$this->_findLanguageFile()) {
|
65
|
$msg = 'unable to find valid language definition file in<br />';
|
66
|
$msg .= '"'.str_replace($sBasePath, '', $this->_sLanguageDirectory).'"';
|
67
|
throw new TranslationException($msg);
|
68
|
}
|
69
|
$this->_importArrays();
|
70
|
}
|
71
|
$this->_bLoaded = (sizeof($this->_LanguageTable) > 0);
|
72
|
}
|
73
|
/**
|
74
|
* return requested translation for a key
|
75
|
* @param string $sLanguageKey 2-uppercase letters language code
|
76
|
* @return string found translation or empty string
|
77
|
*/
|
78
|
public function __get($sLanguageKey)
|
79
|
{
|
80
|
$sRetval = (isset($this->_LanguageTable[$sLanguageKey])
|
81
|
? $this->_LanguageTable[$sLanguageKey] : '{missing: '.$sLanguageKey.'}');
|
82
|
return $sRetval;
|
83
|
}
|
84
|
/**
|
85
|
* returns the whoole language array for use in templateengine
|
86
|
* @return array
|
87
|
*/
|
88
|
public function getLangArray()
|
89
|
{
|
90
|
return $this->_LanguageTable;
|
91
|
}
|
92
|
/**
|
93
|
* search language file in order: LANGUAGE - DEFAULT_LANGUAGE - FIRST_FOUND
|
94
|
* @return boolean
|
95
|
*/
|
96
|
private function _findLanguageFile()
|
97
|
{
|
98
|
$bMatch = false;
|
99
|
$dir = $this->_sLanguageDirectory;
|
100
|
if(is_readable($dir.$this->_sCurrentLanguage.'.php')) {
|
101
|
// check actual language
|
102
|
$this->_sLanguageFile = $dir.$this->_sCurrentLanguage.'.php';
|
103
|
$bMatch = true;
|
104
|
}else {
|
105
|
if(is_readable($dir.$this->_sDefaultLanguage.'.php')) {
|
106
|
// check default language
|
107
|
$this->_sLanguageFile = $dir.$this->_sDefaultLanguage.'.php';
|
108
|
$bMatch = true;
|
109
|
}else {
|
110
|
// search for first available and readable language file
|
111
|
if(is_readable($dir)) {
|
112
|
$iterator = new DirectoryIterator($dir);
|
113
|
foreach ($iterator as $fileinfo) {
|
114
|
if(!preg_match('/^[A-Z]{2}\.php$/', $fileinfo->getBasename())) { continue; }
|
115
|
$sLanguageFile = str_replace('\\', '/', $fileinfo->getPathname());
|
116
|
if(is_readable($sLanguageFile)) {
|
117
|
$this->_sLanguageFile = $sLanguageFile;
|
118
|
$bMatch = true;
|
119
|
break;
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
return $bMatch;
|
126
|
}
|
127
|
/**
|
128
|
* import key-values from language file
|
129
|
*/
|
130
|
private function _importArrays()
|
131
|
{
|
132
|
include($this->_sLanguageFile);
|
133
|
$aLangSections = array('HEADING', 'TEXT', 'MESSAGE', 'MENU', 'OVERVIEW', 'GENERIC');
|
134
|
foreach($aLangSections as $sSection) {
|
135
|
if(isset(${$sSection}) && is_array(${$sSection})) {
|
136
|
foreach(${$sSection} as $key => $value) {
|
137
|
$this->_LanguageTable[$sSection.'_'.$key] = $value;
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
} // end class Translate
|
143
|
/**
|
144
|
* Exception class for Translation
|
145
|
*/
|
146
|
class TranslationException extends AppException {}
|
147
|
|