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