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