/[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.6 - (hide annotations)
Sun Feb 9 17:33:54 2003 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.5: +12 -14 lines
+ minor update related to new log level constants

1 joko 1.1 <?
2     // -----------------------------------------------------------------
3 joko 1.6 // $Id: LocaleText.php,v 1.5 2002/12/19 10:30:06 joko Exp $
4 joko 1.1 // ------------------------------------------------------------------
5 joko 1.2 // $Log: LocaleText.php,v $
6 joko 1.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 joko 1.5 // Revision 1.4 2002/12/19 06:18:40 joko
10     // + '$this->site' gets used here now
11     //
12 joko 1.4 // Revision 1.3 2002/12/04 07:40:05 jonen
13     // + comment out dprint
14     //
15 jonen 1.3 // Revision 1.2 2002/12/03 03:10:21 joko
16     // + bugfix regarding new object hierarchy
17     //
18 joko 1.2 // Revision 1.1 2002/11/12 05:42:30 joko
19     // + initial checkin
20     //
21 joko 1.1 // 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 joko 1.4
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 joko 1.6 global $app, $slt;
73 joko 1.2 session_register_safe('slt');
74 joko 1.6 $this->langs_avail = $app->getConfig("languages.available");
75 joko 1.1 $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 joko 1.6
109     // TODO: refactor this to Data::Deep
110 joko 1.1 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 joko 1.6 // TODO: refactor this to Data::Deep
124 joko 1.5 function _getldsvar($ldskey, $tpl=array(), $newMode = 0) {
125 joko 1.4
126     //print "ldskey: $ldskey<br>";
127    
128 joko 1.1 $var_name = '$this->lds' . $ldskey;
129     $eval_string = "return $var_name;";
130     //print $eval_string . "<br>";
131 joko 1.4
132 joko 1.1 // interpolate variable-name
133     $var_value = eval($eval_string);
134     // interpolate variable-value to do some template-vars
135 joko 1.5 if (!$newMode) {
136     $var_value = eval("return \"$var_value\";");
137     }
138 joko 1.4
139     // V1 - result interpolation using eval
140 joko 1.1 // print $eval_string."<br>";
141     //$var_value = $$var_name;
142 joko 1.4
143     // V2 - result interpolation using smarty
144     //print Dumper($tpl);
145     //$this->site->loadTemplate();
146    
147 joko 1.1 return $var_value;
148     }
149    
150 joko 1.6 // TODO: refactor this to Data::Deep
151 joko 1.1 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 joko 1.4 //global $site;
162 joko 1.6 $this->site->log( get_class($this) . "->_loadStructure_FromDb loading language information", PEAR_LOG_DEBUG);
163 joko 1.1 //print "- loading structure from db ($this->langkey)<br>";
164     //connectdb();
165     $sql = "SELECT * FROM td_res_langtext WHERE countrykey='" . $this->langkey . "'";
166 joko 1.4 if ($result = $this->site->db->query($sql)) {
167 joko 1.1 //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 joko 1.6 $this->site->log( get_class($this) . "->_loadStructure_FromDb ready", PEAR_LOG_DEBUG);
175 joko 1.1 }
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 joko 1.2 //print "setting lang<br>";
189     if ($langkey_new) {
190 joko 1.1 $slt[langkey] = $langkey_new;
191     }
192 joko 1.2 $this->load();
193 joko 1.1 }
194    
195     function load() {
196     global $slt;
197    
198 joko 1.2 if (!$this->langkey) { $this->langkey = $slt[langkey]; }
199 joko 1.1 if (!$this->langkey) { $this->langkey = $this->_findAvailableLanguage($slt[langkey]); }
200    
201 joko 1.2 //print "- starting language-library<br>";
202     //print "lang: " . $this->langkey . "<br>";
203 joko 1.1 //$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 joko 1.5 function getlt($key, $tpl=array(), $newMode = 0) {
223 jonen 1.3 //dprint("getlt: $key");
224 joko 1.1 if ($ldskey = $this->_dbkey2ldskey($key)) {
225 joko 1.5 return $this->_getldsvar($ldskey, $tpl, $newMode);
226 joko 1.1 }
227     }
228    
229     }
230    
231 joko 1.4
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 joko 1.1 ?>

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