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                 $i->ltype = 'l';
120                 $i->applyFilters($_REQUEST, $this->authUser, $this);
121                 $this->jdata($i->toTransList('l',  $lbits[0]));
122                 break;
123
124             case 'Country':
125                 $i->ltype = 'c';
126                 $i->applyFilters($_REQUEST, $this->authUser, $this);
127                 $this->jdata($i->toTransList('c',  $lbits[0]));
128                
129                 break;
130                 
131             case 'Currency':
132                 $i->ltype = 'm';
133                 $i->applyFilters($_REQUEST, $this->authUser, $this);
134                 $this->jdata($i->toTransList('m',  $lbits[0]));
135                 break;
136             
137             case 'Timezone':
138                 $ar = DateTimeZone::listAbbreviations();
139                 $ret = array();
140                 $tza = array();
141                 foreach($ar as $tl => $sar) {
142                     foreach($sar as $tz) {
143                         $tza[]  = $tz['timezone_id'];
144                     
145                     }
146                 }
147                 $tza= array_unique($tza);
148                 sort($tza);
149                 foreach($tza as $tz) {
150                     $ret[] = array('tz' => $tz);
151                 }
152                 $this->jdata($ret);
153                 
154                 
155                 
156              
157                 
158         }
159         if (!empty($_REQUEST['debug'])) {
160             DB_DataObject::debugLevel(1);
161         }
162         $i = DB_DataObject::Factory('I18n');
163         $i->buildDb();
164       
165        
166         $i = DB_DataObject::Factory('I18n');
167         $cfg = $i->cfg();
168         $langs = $cfg['t'];
169        // var_dump($langs);exit;
170         $ar = array();
171         foreach($langs as $lang)
172         {
173             $rlang = array_shift(explode('_', strtoupper($lang)));
174             
175             $ar[$lang] = array();
176             $i = DB_DataObject::Factory('I18n');
177             $ar[$lang]['l'] = $i->toTransList('l',  $rlang);
178             $i = DB_DataObject::Factory('I18n');
179             $ar[$lang]['c'] =  $i->toTransList('c', $rlang);
180             $i = DB_DataObject::Factory('I18n');
181             $ar[$lang]['m'] = $i->toTransList('m', $rlang);
182         }
183         //echo '<PRE>';print_r($ar);
184         header('Content-type: text/javascript');
185         echo 'Pman.I18n.Data = ' .  json_encode($ar);
186         exit;
187         
188         
189         
190     }
191     
192  
193     
194      /**
195      * translate (used by database building);
196      * usage :
197      * require_once 'Pman/Core/I18N.php';
198      * $x = new Pman_Core_I18N();
199      * $x->translate($this->authuser, 'c', 'US');
200      * @param au - auth User
201      * @param type = 'c' or 'l'
202      * @param k - key to translate
203      * 
204      */
205      
206     function translate($au, $type, $k) 
207     {
208       
209         static $cache;
210         if (empty($k)) {
211             return '??';
212         }
213         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
214         
215         // does it need caching?
216         
217         $i = DB_DataObject::Factory('I18n');
218         return $i->translate($lang,$type,$k);
219         
220         
221          
222         
223         
224     }
225     /**
226      * translate a list of items
227      * @param Pman_Core_DataObjects_Person $au Authenticated user
228      * @param String                      $type  c/l/m
229      * @param String                      $k     'comma' seperated list of keys to translate
230      */
231     
232     function translateList($au, $type, $k)  
233     {
234         $ar = explode(',', $k);
235         $ret = array();
236         foreach($ar as $kk) {
237             $ret[] = $this->translate($au, $type, $kk);
238         }
239         return implode(', ', $ret);
240     }
241     /**
242      * convert rate:
243      * usage  : $i = new Pman_Core_I18n();
244      * $ret = $i->convertCurrency(100,"HKD","USD");
245      * if ($ret == false) {
246             /// something went wrong.
247      }
248      * 
249      * @param Pman_Core_DataObjects_Person $au Authenticated user
250      * @param String                      $type  c/l/m
251      * @param String                      $k     'comma' seperated list of keys to translate
252      */
253     
254     
255     function convertCurrency($val, $from, $to)
256     {
257         $r = $this->loadRates();
258         if ($r === false) {
259             return false;
260         }
261         if (!isset($this->rates[$from]) || !isset($this->rates[$to]) ) {
262             return false;
263         }
264         //echo '<PRE>';print_R($this->rates);
265         $base = (1.0 / $this->rates[$from]) * $val;
266   
267         return $this->rates[$to] * $base;
268     
269     }
270     var $rates = array();
271     function loadRates()
272     {
273         if (!empty($this->rates)) {
274             return true;
275         }
276         $target = ini_get('session.save_path').'/eurofxref-daily.xml';
277         if (!file_exists($target) || filemtime($target) < (time() - 60*60*24)) {
278             $f = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
279             if (!strlen($f)) {
280                 return false;
281             }
282             file_put_contents($target,$f);
283         } 
284         $dom = simplexml_load_file($target);
285         $this->rates['EUR'] = 1.0;
286         $this->rates['TWD'] = 46.7008412;
287         $this->rates['VND'] = 26405.3;
288        
289         foreach($dom->Cube->Cube->Cube as $c) {
290            //echo '<PRE>';print_r($c );
291             $this->rates[(string)$c['currency']] = (string)$c['rate'];
292         }
293          $this->rates['RMB'] = $this->rates['CNY'] ;
294     }
295     
296     
297      
298     
299 }