1 |
<? |
2 |
// ----------------------------------------------------------------- |
3 |
// $Id: LocaleText.php,v 1.6 2003/02/09 17:33:54 joko Exp $ |
4 |
// ------------------------------------------------------------------ |
5 |
// $Log: LocaleText.php,v $ |
6 |
// Revision 1.6 2003/02/09 17:33:54 joko |
7 |
// + minor update related to new log level constants |
8 |
// |
9 |
// Revision 1.5 2002/12/19 10:30:06 joko |
10 |
// + argument 'newMode' for function getlt and function _getldsvar: let _getldsvar evaluate variable to still provide old behaviour/functionality |
11 |
// |
12 |
// Revision 1.4 2002/12/19 06:18:40 joko |
13 |
// + '$this->site' gets used here now |
14 |
// |
15 |
// Revision 1.3 2002/12/04 07:40:05 jonen |
16 |
// + comment out dprint |
17 |
// |
18 |
// Revision 1.2 2002/12/03 03:10:21 joko |
19 |
// + bugfix regarding new object hierarchy |
20 |
// |
21 |
// Revision 1.1 2002/11/12 05:42:30 joko |
22 |
// + initial checkin |
23 |
// |
24 |
// Revision 1.9 2002/10/25 11:47:50 cvsmax |
25 |
// + modificate function getlt() to get variables in language related content work |
26 |
// |
27 |
// Revision 1.8 2002/07/27 00:31:10 cvsjoko |
28 |
// *** empty log message *** |
29 |
// |
30 |
// Revision 1.7 2002/07/19 18:17:30 cvsjoko |
31 |
// + changes for pre r0.01 |
32 |
// |
33 |
// Revision 1.6 2002/06/27 02:27:57 cvsjoko |
34 |
// + bugfix: couldn't use php-keywords as keys due to lack of quoting |
35 |
// |
36 |
// Revision 1.5 2002/06/25 04:25:28 cvsjoko |
37 |
// + added error handling: load structure only if connection to db possible |
38 |
// |
39 |
// Revision 1.4 2002/06/24 14:28:59 cvsmax |
40 |
// + bugfixing |
41 |
// |
42 |
// Revision 1.3 2002/06/23 05:23:02 cvsmax |
43 |
// + add func 'user-session timeout' and extended (user) session logging |
44 |
// |
45 |
// Revision 1.2 2002/06/19 18:16:14 cvsmax |
46 |
// + import to cvs |
47 |
// |
48 |
// ------------------------------------------------------------------ |
49 |
|
50 |
|
51 |
class LocaleText { |
52 |
|
53 |
// current selected language |
54 |
var $langkey; |
55 |
|
56 |
// language-data-structure (hash) |
57 |
var $lds; |
58 |
|
59 |
// available languages |
60 |
var $langs_avail; |
61 |
|
62 |
// ---------------------------------------------------- |
63 |
function LocaleText() { |
64 |
|
65 |
// look at the bottom of this file!!! |
66 |
//$smarty->register_block("translate", "do_translation"); |
67 |
//$this->site->smarty->register_block("translate", "_do_translation"); |
68 |
|
69 |
//global $site; |
70 |
//$site->smarty->register_block("translate", "_do_translation"); |
71 |
|
72 |
} |
73 |
|
74 |
function start() { |
75 |
global $app, $slt; |
76 |
session_register_safe('slt'); |
77 |
$this->langs_avail = $app->getConfig("languages.available"); |
78 |
$this->load(); |
79 |
} |
80 |
|
81 |
// ---------------------------------------------------- |
82 |
function _detectLanguage() { |
83 |
// detect language from Browser |
84 |
// HACK |
85 |
$browserLang = "tr"; |
86 |
// if there is a browser-language set the current language to this one |
87 |
if ($browserLang) { |
88 |
// TODO: log this |
89 |
//print "- detecting browser-language: $browserLang<br>"; |
90 |
return $browserLang; |
91 |
} |
92 |
} |
93 |
|
94 |
// ---------------------------------------------------- |
95 |
function _findAvailableLanguage($newlang = "") { |
96 |
// check against available languages ... |
97 |
$langs_avail = $this->langs_avail; |
98 |
if (!( is_array($langs_avail) && count($langs_avail) > 0 )) { |
99 |
// TODO: log this |
100 |
print "error with multi-language-system, maybe \"GLBL_LANGSAVAIL\" is not properly configured?<br>"; |
101 |
return; |
102 |
} |
103 |
if (in_array($newlang, $langs_avail)) { |
104 |
// ... take the new one, if available, ... |
105 |
return $newlang; |
106 |
} else { |
107 |
// ... or take the first one as a default |
108 |
return $langs_avail[0]; |
109 |
} |
110 |
} |
111 |
|
112 |
// TODO: refactor this to Data::Deep |
113 |
function _dbkey2ldskey($dbkey) { |
114 |
// TODO: log this |
115 |
//dprint("dbkey: $dbkey"); |
116 |
//if (!$dbkey) { return; } |
117 |
if(substr($dbkey,0,1)=="/") { $dbkey=substr($dbkey,1); } |
118 |
$dbkey_arr = split('/', $dbkey); |
119 |
$ldskey = "['" . join("']['", $dbkey_arr) . "']"; |
120 |
// return only if key is valid for php!!! |
121 |
if (!stristr($ldskey, '[]')) { |
122 |
return $ldskey; |
123 |
} |
124 |
} |
125 |
|
126 |
// TODO: refactor this to Data::Deep |
127 |
function _getldsvar($ldskey, $tpl=array(), $newMode = 0) { |
128 |
|
129 |
//print "ldskey: $ldskey<br>"; |
130 |
|
131 |
$var_name = '$this->lds' . $ldskey; |
132 |
$eval_string = "return $var_name;"; |
133 |
//print $eval_string . "<br>"; |
134 |
|
135 |
// interpolate variable-name |
136 |
$var_value = eval($eval_string); |
137 |
// interpolate variable-value to do some template-vars |
138 |
if (!$newMode) { |
139 |
$var_value = eval("return \"$var_value\";"); |
140 |
} |
141 |
|
142 |
// V1 - result interpolation using eval |
143 |
// print $eval_string."<br>"; |
144 |
//$var_value = $$var_name; |
145 |
|
146 |
// V2 - result interpolation using smarty |
147 |
//print Dumper($tpl); |
148 |
//$this->site->loadTemplate(); |
149 |
|
150 |
return $var_value; |
151 |
} |
152 |
|
153 |
// TODO: refactor this to Data::Deep |
154 |
function _setldsvar($ldskey, $val) { |
155 |
$var_name = '$this->lds' . $ldskey; |
156 |
$value_new = addslashes($val); |
157 |
$eval_string = "$var_name = '$value_new';"; |
158 |
//print $eval_string . "<br>"; |
159 |
eval($eval_string); |
160 |
//$$var_name = $value_new; |
161 |
} |
162 |
|
163 |
function _loadStructure_FromDb() { |
164 |
$this->site->log( get_class($this) . "->_loadStructure_FromDb: Loading language information (l10n) for '$this->langkey'.", PEAR_LOG_DEBUG); |
165 |
$sql = "SELECT * FROM td_res_langtext WHERE countrykey='" . $this->langkey . "'"; |
166 |
if ($result = $this->site->db->query($sql)) { |
167 |
|
168 |
// pre-flight check: Is $result an object of class "db_error"? |
169 |
if (get_class($result) == 'db_error') { |
170 |
// FIXME: How to handle this here??? |
171 |
//print "Sorry.<br/>"; |
172 |
$this->site->log( get_class($this) . "->_loadStructure_FromDb: Error while issuing '$sql'.", PEAR_LOG_ERR); |
173 |
return; |
174 |
} |
175 |
|
176 |
//while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { |
177 |
//print Dumper($result); |
178 |
while ( $result->fetchInto($row) ) { |
179 |
//print $row[tval]."<br>"; |
180 |
//print_r($row) . "<br>"; |
181 |
$this->_loadStructure_FromDb_add($row["tkey"], $row["tval"]); |
182 |
} |
183 |
} |
184 |
$this->site->log( get_class($this) . "->_loadStructure_FromDb ready", PEAR_LOG_DEBUG); |
185 |
} |
186 |
|
187 |
function _loadStructure_FromDb_add($dbkey, $val) { |
188 |
$ldskey = $this->_dbkey2ldskey($dbkey); |
189 |
$this->_setldsvar($ldskey, $val); |
190 |
} |
191 |
|
192 |
|
193 |
|
194 |
// ======================================== |
195 |
|
196 |
function setLanguage($langkey_new = "") { |
197 |
global $slt; |
198 |
//print "setting lang<br>"; |
199 |
if ($langkey_new) { |
200 |
$slt[langkey] = $langkey_new; |
201 |
} |
202 |
$this->load(); |
203 |
} |
204 |
|
205 |
function load() { |
206 |
global $slt; |
207 |
|
208 |
if (!$this->langkey) { $this->langkey = $slt[langkey]; } |
209 |
if (!$this->langkey) { $this->langkey = $this->_findAvailableLanguage($slt[langkey]); } |
210 |
|
211 |
//print "- starting language-library<br>"; |
212 |
//print "lang: " . $this->langkey . "<br>"; |
213 |
//$langkey_new = $this->_detectLanguage(); |
214 |
//$this->_loadStructure_FromDb(); |
215 |
//$this->setLanguage($langkey_new); |
216 |
|
217 |
$this->_loadStructure_FromDb(); |
218 |
} |
219 |
|
220 |
function getAvailableLanguages() { |
221 |
return $this->langs_avail; |
222 |
} |
223 |
|
224 |
function getCurrentLanguage() { |
225 |
return $this->langkey; |
226 |
} |
227 |
|
228 |
function getLangTexts() { |
229 |
return $this->lds; |
230 |
} |
231 |
|
232 |
function getlt($key, $tpl=array(), $newMode = 0) { |
233 |
//dprint("getlt: $key"); |
234 |
if ($ldskey = $this->_dbkey2ldskey($key)) { |
235 |
return $this->_getldsvar($ldskey, $tpl, $newMode); |
236 |
} |
237 |
} |
238 |
|
239 |
} |
240 |
|
241 |
|
242 |
|
243 |
/* |
244 |
Example 13-17. register_block |
245 |
taken from: http://smarty.php.net/manual/en/api.register.block.html |
246 |
will be triggered by this template-code: |
247 |
{* template *} |
248 |
{translate lang="br"} |
249 |
Hello, world! |
250 |
{/translate} |
251 |
*/ |
252 |
|
253 |
/* PHP */ |
254 |
//global $smarty; |
255 |
//$smarty->register_block("translate", "do_translation"); |
256 |
|
257 |
/* |
258 |
function _do_translation ($params, $content, &$smarty) { |
259 |
print "do_translation<br>"; |
260 |
print "params: " . Dumper($params) . "<br>"; |
261 |
print "content: $content<br>"; |
262 |
if ($content) { |
263 |
//$lang = $params['lang']; |
264 |
// do some translation with $content |
265 |
//echo $translation; |
266 |
return getlt($content); |
267 |
} |
268 |
} |
269 |
*/ |
270 |
|
271 |
?> |