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         $this->sessionState(0);
112         $lbits = $this->guessUsersLanguage();
113          
114         if ($this->authUser && !empty($_REQUEST['_debug'])) {
115             DB_DataObject::debugLevel(1);
116         }
117         
118         
119         
120         $i = DB_DataObject::Factory('I18n');
121         
122         switch($s) {
123             case 'Lang':
124                  
125                 
126                 $i->ltype = 'l';
127                 $i->applyFilters($_REQUEST, $this->authUser, $this);
128                 $this->jdata($i->toTransList('l',  implode('_',$lbits)));
129                 break;
130
131             case 'Country':
132                 $i->ltype = 'c';
133                 $i->applyFilters($_REQUEST, $this->authUser, $this);
134                 $this->jdata($i->toTransList('c',  implode('_',$lbits)));
135                
136                 break;
137                 
138             case 'Currency':
139                 $i->ltype = 'm';
140                 $i->applyFilters($_REQUEST, $this->authUser, $this);
141                 $this->jdata($i->toTransList('m',  implode('_',$lbits)));
142                 break;
143             
144             case 'Timezone':
145                 $ar = DateTimeZone::listAbbreviations();
146                 $ret = array();
147                 $tza = array();
148                 foreach($ar as $tl => $sar) {
149                     foreach($sar as $tz) {
150                         $tza[]  = $tz['timezone_id'];
151                     
152                     }
153                 }
154                 $tza= array_unique($tza);
155                 sort($tza);
156                 foreach($tza as $tz) {
157                     //filtering..
158                     if (empty($_REQUEST['q']) ||
159                             0 === strcasecmp(
160                                     substr($tz,0, strlen($_REQUEST['q'])),
161                                     $_REQUEST['q'])
162                     ) {
163                         $ret[] = array('tz' => $tz);
164                     }
165                     
166                 }
167                 $this->jdata($ret);
168                 
169                 
170                 
171              
172                 
173         }
174         if (!empty($_REQUEST['debug'])) {
175             DB_DataObject::debugLevel(1);
176         }
177         
178         $i = DB_DataObject::Factory('I18n');
179         $i->buildDB();
180       
181        
182         $i = DB_DataObject::Factory('I18n');
183         $cfg = $i->cfg();
184         $langs = $cfg['t'];
185        // var_dump($langs);exit;
186         $ar = array();
187         foreach($langs as $lang)
188         {
189             //$rlang = array_shift(explode('_', strtoupper($lang)));
190             $rlang = array_shift(explode('_', $lang));
191             
192             $ar[$lang] = array();
193             $i = DB_DataObject::Factory('I18n');
194             $ar[$lang]['l'] = $i->toTransList('l',  $rlang);
195             $i = DB_DataObject::Factory('I18n');
196             $ar[$lang]['c'] =  $i->toTransList('c', $rlang);
197             $i = DB_DataObject::Factory('I18n');
198             $ar[$lang]['m'] = $i->toTransList('m', $rlang);
199         }
200         //echo '<PRE>';print_r($ar);
201         header('Content-type: text/javascript');
202         echo 'Pman.I18n.Data = ' .  json_encode($ar);
203         exit;
204         
205         
206         
207     }
208     
209  
210     
211      /**
212      * translate (used by database building);
213      * usage :
214      * require_once 'Pman/Core/I18n.php';
215      * $x = new Pman_Core_I18N();
216      * $x->translate($this->authuser, 'c', 'US');
217      * @param au - auth User
218      * @param type = 'c' or 'l'
219      * @param k - key to translate
220      * 
221      */
222      
223     function translate($au, $type, $k) 
224     {
225       
226         static $cache;
227         if (empty($k)) {
228             return '??';
229         }
230         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
231         
232         // does it need caching?
233         
234         $i = DB_DataObject::Factory('I18n');
235         return $i->translate($lang,$type,$k);
236         
237         
238          
239         
240         
241     }
242     /**
243      * translate a list of items
244      * @param Pman_Core_DataObjects_Person $au Authenticated user
245      * @param String                      $type  c/l/m
246      * @param String                      $k     'comma' seperated list of keys to translate
247      */
248     
249     function translateList($au, $type, $k)  
250     {
251         $ar = explode(',', $k);
252         $ret = array();
253         foreach($ar as $kk) {
254             $ret[] = $this->translate($au, $type, $kk);
255         }
256         return implode(', ', $ret);
257     }
258     /**
259      * DO NOT USE THIS -- see core_curr_rates dataobject.
260      */
261     
262     
263     function convertCurrency($val, $from, $to)
264     {
265         
266         return DB_DAtaObject::Factory('core_curr_rate')->convert($val,$from,$to);
267     
268     }
269     /**
270      * DO NOT USE THIS -- see core_curr_rates dataobject.
271      *
272      */
273     
274     static $rates = array();
275     
276     function loadRates()
277     {   
278         if (!empty($this->rates)) {
279             return true;
280         }
281         
282         $this->rates = DB_DAtaObject::Factory('core_curr_rate')->currentRates();
283         
284         
285     }
286     
287     
288      
289     
290 }