/[cvs]/nfo/php/libs/org.netfrag.flib/Application/l10n/LocaleText.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.flib/Application/l10n/LocaleText.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Thu Dec 19 10:30:06 2002 UTC (21 years, 9 months ago) by joko
Branch: MAIN
Changes since 1.4: +10 -5 lines
+ argument 'newMode' for function getlt and function _getldsvar: let _getldsvar evaluate variable to still provide old behaviour/functionality

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed