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