I18n.php
[Pman.Core] / I18n.php
1 <?php
2 /**
3  * 
4  * Either we load this as a standard array at start??
5  * eg. after login...
6  * 
7  * We basically load up all supported languages at the start of the application.
8  * 
9  * By default it returns
10  * 
11  * Pman.I18n.Data = {
12       
13       en : {
14           l :  [ { code : 'en', title : 'English' }, .... ],
15           c :  [ { code : 'UK', title : 'United Kingdom' }, .... ],
16           m :  [ { code : 'USD', title : 'US Dollars' }, .... ],
17       fr : ....
18    }
19  * 
20  * 
21  * other usage:
22  * 
23  * index.php/Pman/I18N/BuildDB -- buildes the database..
24  * .. other formats are depreciated, but are supported by the old code....
25  * 
26  * 
27  * Database language translation should be done using the database table.
28  * So sorting can be done correctly..
29  * 
30  * Configuration in index.php..
31  * 
32  *  'Pman_Core_I18N' => array(
33       'l' => array(
34             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
35             'id', // indonesian
36             'tl', // tagalog
37             'vi', //vietnamise
38             'hi', // hindi
39             'ta', // tamil
40             '**', // other
41         ), 
42        'c' => '*', // eg. all languages..
43        'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
44     ), 
45
46  * 
47  */
48 require_once 'Pman.php';
49
50 class Pman_Core_I18n extends Pman
51 {
52  
53     
54      
55     
56      
57     
58     
59     function getAuth()
60     {
61         parent::getAuth(); // load company!
62         //return true;
63         $au = $this->getAuthUser();
64         //if (!$au) {
65         //    $this->jerr("Not authenticated", array('authFailure' => true));
66         //}
67         $this->authUser = $au;
68         
69         $ff= HTML_FlexyFramework::get();
70          
71         
72         $opts = empty($ff->Pman_Core_I18N) ?
73                 (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)
74                 : $ff->Pman_Core_I18N;
75         
76          
77         
78         
79         
80         return true;
81     }
82      
83     
84     function guessUsersLanguage() {
85          
86         $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
87         
88         /// verify the selected language..
89         $i = DB_DataObject::Factory('I18n');
90         $i->ltype = 'l';                           // string(1)  not_null multiple_key
91         $i->lkey = $lang;                            // string(8)  not_null
92         if (!$i->count()) {    
93             $i = DB_DataObject::Factory('I18n');
94             $i->buildDb();
95             
96             $i = DB_DataObject::Factory('I18n');
97             $i->ltype = 'l';                           // string(1)  not_null multiple_key
98             $i->lkey = $lang;  
99             if (!$i->count()) { 
100                 $this->jerr('invalid lang configured: ' . $lang);
101             }
102         }
103         
104         
105         return explode('_', $lang);
106     }
107      
108     function get($s ='')
109     {
110      
111      
112         $lbits = $this->guessUsersLanguage();
113          
114         
115         $i = DB_DataObject::Factory('I18n');
116         
117         switch($s) {
118             case 'Lang':
119                  
120                 
121                 $i->ltype = 'l';
122                 $i->applyFilters($_REQUEST, $this->authUser, $this);
123                 $this->jdata($i->toTransList('l',  implode('_',$lbits)));
124                 break;
125
126             case 'Country':
127                 $i->ltype = 'c';
128                 $i->applyFilters($_REQUEST, $this->authUser, $this);
129                 $this->jdata($i->toTransList('c',  implode('_',$lbits)));
130                
131                 break;
132                 
133             case 'Currency':
134                 $i->ltype = 'm';
135                 $i->applyFilters($_REQUEST, $this->authUser, $this);
136                 $this->jdata($i->toTransList('m',  implode('_',$lbits)));
137                 break;
138             
139             case 'Timezone':
140                 $ar = DateTimeZone::listAbbreviations();
141                 $ret = array();
142                 $tza = array();
143                 foreach($ar as $tl => $sar) {
144                     foreach($sar as $tz) {
145                         $tza[]  = $tz['timezone_id'];
146                     
147                     }
148                 }
149                 $tza= array_unique($tza);
150                 sort($tza);
151                 foreach($tza as $tz) {
152                     //filtering..
153                     if (empty($_REQUEST['q']) ||
154                             0 === strcasecmp(
155                                     substr($tz,0, strlen($_REQUEST['q'])),
156                                     $_REQUEST['q'])
157                     ) {
158                         $ret[] = array('tz' => $tz);
159                     }
160                     
161                 }
162                 $this->jdata($ret);
163                 
164                 
165                 
166              
167                 
168         }
169         if (!empty($_REQUEST['debug'])) {
170             DB_DataObject::debugLevel(1);
171         }
172         
173         $i = DB_DataObject::Factory('I18n');
174         $i->buildDB();
175       
176        
177         $i = DB_DataObject::Factory('I18n');
178         $cfg = $i->cfg();
179         $langs = $cfg['t'];
180        // var_dump($langs);exit;
181         $ar = array();
182         foreach($langs as $lang)
183         {
184             //$rlang = array_shift(explode('_', strtoupper($lang)));
185             $rlang = array_shift(explode('_', $lang));
186             
187             $ar[$lang] = array();
188             $i = DB_DataObject::Factory('I18n');
189             $ar[$lang]['l'] = $i->toTransList('l',  $rlang);
190             $i = DB_DataObject::Factory('I18n');
191             $ar[$lang]['c'] =  $i->toTransList('c', $rlang);
192             $i = DB_DataObject::Factory('I18n');
193             $ar[$lang]['m'] = $i->toTransList('m', $rlang);
194         }
195         //echo '<PRE>';print_r($ar);
196         header('Content-type: text/javascript');
197         echo 'Pman.I18n.Data = ' .  json_encode($ar);
198         exit;
199         
200         
201         
202     }
203     
204  
205     
206      /**
207      * translate (used by database building);
208      * usage :
209      * require_once 'Pman/Core/I18n.php';
210      * $x = new Pman_Core_I18N();
211      * $x->translate($this->authuser, 'c', 'US');
212      * @param au - auth User
213      * @param type = 'c' or 'l'
214      * @param k - key to translate
215      * 
216      */
217      
218     function translate($au, $type, $k) 
219     {
220       
221         static $cache;
222         if (empty($k)) {
223             return '??';
224         }
225         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
226         
227         // does it need caching?
228         
229         $i = DB_DataObject::Factory('I18n');
230         return $i->translate($lang,$type,$k);
231         
232         
233          
234         
235         
236     }
237     /**
238      * translate a list of items
239      * @param Pman_Core_DataObjects_Person $au Authenticated user
240      * @param String                      $type  c/l/m
241      * @param String                      $k     'comma' seperated list of keys to translate
242      */
243     
244     function translateList($au, $type, $k)  
245     {
246         $ar = explode(',', $k);
247         $ret = array();
248         foreach($ar as $kk) {
249             $ret[] = $this->translate($au, $type, $kk);
250         }
251         return implode(', ', $ret);
252     }
253     /**
254      * convert rate:
255      * usage  : $i = new Pman_Core_I18n();
256      * $ret = $i->convertCurrency(100,"HKD","USD");
257      * if ($ret == false) {
258             /// something went wrong.
259      }
260      * 
261      * @param Pman_Core_DataObjects_Person $au Authenticated user
262      * @param String                      $type  c/l/m
263      * @param String                      $k     'comma' seperated list of keys to translate
264      */
265     
266     
267     function convertCurrency($val, $from, $to)
268     {
269         $r = $this->loadRates();
270         if ($r === false) {
271             return false;
272         }
273         if (!isset($this->rates[$from]) || !isset($this->rates[$to]) ) {
274             return false;
275         }
276         //echo '<PRE>';print_R($this->rates);
277         $base = (1.0 / $this->rates[$from]) * $val;
278   
279         return $this->rates[$to] * $base;
280     
281     }
282     var $rates = array();
283     function loadRates()
284     {
285         if (!empty($this->rates)) {
286             return true;
287         }
288         $target = ini_get('session.save_path').'/eurofxref-daily.xml';
289         if (!file_exists($target) || filemtime($target) < (time() - 60*60*24)) {
290             $f = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
291             if (!strlen($f)) {
292                 return false;
293             }
294             file_put_contents($target,$f);
295         } 
296         $dom = simplexml_load_file($target);
297         $this->rates['EUR'] = 1.0;
298         $this->rates['TWD'] = 46.7008412;
299         $this->rates['VND'] = 26405.3;
300        
301         foreach($dom->Cube->Cube->Cube as $c) {
302            //echo '<PRE>';print_r($c );
303             $this->rates[(string)$c['currency']] = (string)$c['rate'];
304         }
305          $this->rates['RMB'] = $this->rates['CNY'] ;
306     }
307     
308     
309      
310     
311 }