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