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