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