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       en : [ { code : 'end', title : 'English' }, .... ],
13       fr : ....
14    }
15  * 
16  * 
17  * other usage:
18  * 
19  * index.php/Pman/I18N/BuildDB -- buildes the database..
20  * .. other formats are depreciated, but are supported by the old code....
21  * 
22  * 
23  * Database language translation should be done using the database table.
24  * So sorting can be done correctly..
25  * 
26  * Configuration in index.php..
27  * 
28  *  'Pman_Core_I18N' => array(
29       'l' => array(
30             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
31             'id', // indonesian
32             'tl', // tagalog
33             'vi', //vietnamise
34             'hi', // hindi
35             'ta', // tamil
36             '**', // other
37         ), 
38        'c' => '*', // eg. all languages..
39        'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
40     ), 
41
42  * 
43  */
44 require_once 'Pman.php';
45
46 class Pman_Core_i18N extends Pman
47 {
48  
49     
50     // these are the default languages we support.
51     var $cfg = array(
52         'l' => array(
53             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
54             'id', // indonesian
55             'tl', // tagalog
56             'vi', //vietnamise
57             'hi', // hindi
58             'ta', // tamil
59             '**', // other
60         ),
61         'c' => array(
62              'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
63         ),
64         'm' => array(
65             'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
66         )
67     );
68     
69     
70      
71     
72     
73     function getAuth()
74     {
75         parent::getAuth(); // load company!
76         //return true;
77         $au = $this->getAuthUser();
78         //if (!$au) {
79         //    $this->jerr("Not authenticated", array('authFailure' => true));
80         //}
81         $this->authUser = $au;
82         
83         $opts = PEAR::getStaticProperty('Pman_Core_I18N', 'options');
84         if (empty($opts)) {
85             $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
86         }
87         $opts = empty($opts)  ?  array() : $opts;
88         
89         // load the cofiguration
90         foreach($opts as $k=>$v) {
91             
92             if ($v == '*') {
93                 $this->cfg[$k] = $this->getDefaultCfg($k);
94                 continue;
95             }
96             $this->cfg[$k] = is_array($v) ? $v  : explode(',', $v);
97         }
98         
99         
100         
101         
102         return true;
103     }
104     // returns a list of all countries..
105     function getDefaultCfg($t) {
106         $ret = array();
107         switch ($t) {
108             case 'c':
109                 require_once 'I18Nv2/Country.php';
110                 
111                 $c = new I18Nv2_Country('en');
112                 $ret =  array_keys($c->codes);
113                 $ret[] = '**';
114                 break;
115             case 'l':
116                 require_once 'I18Nv2/Language.php';
117                 $c = new I18Nv2_Language('en');
118                 $ret =  array_keys($c->codes);
119                 $ret[] = '**';
120                 break;
121             case 'm':
122                 require_once 'I18Nv2/Currency.php';
123                 $c = new I18Nv2_Currency('en');
124                 $ret =  array_keys($c->codes);
125                 $ret[] = '**';
126                 break;
127         }
128         foreach ($ret as $k=>$v) {
129             $ret[$k] = strtoupper($v);
130         }
131         
132         
133         return $ret;
134     }
135     
136     
137     
138     function setSession($au)
139     {
140         $this->authUser = $au;
141         $lbits = implode('_', $this->findLang());
142         if (empty($_SESSION['Pman_I18N'])) {
143             $_SESSION['Pman_I18N']  = array();
144         }
145         
146         $_SESSION['Pman_I18N'][$lbits] = array(
147             'l' => $this->getList('l', $lbits),
148             'c' => $this->getList('c', $lbits),
149             'm' => $this->getList('m', $lbits),
150         );
151         
152         
153     }
154       
155     function getList($type, $inlang,$fi=false)
156     {
157         //$l = new I18Nv2_Language($inlang);
158         //$c= new I18Nv2_Country($inlang);
159         $filter = !$fi  ? false :  $this->loadFilter($type); // project specific languages..
160        // print_r($filter);
161         
162         $ret = array();
163         
164         
165         
166         
167         foreach($this->cfg[$type] as $k) {
168             if (is_array($filter) && !in_array($k, $filter)) {
169                 continue;
170             }
171              
172             $ret[] = array(
173                 'code'=>$k , 
174                 'title' => $this->translate($inlang, $type, $k)
175             );
176             continue;
177             
178         }
179         // sort it??
180         return $ret;
181         
182     }
183      
184     
185     function findLang() {
186          
187         $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
188         $lbits = explode('_', strtoupper($lang));
189         $lbits[0] = strtolower($lbits[0]);
190         require_once 'I18Nv2/Country.php';
191         require_once 'I18Nv2/Language.php';
192         $langs = new I18Nv2_Language('en');
193         $countries = new I18Nv2_Country('en');
194       //  print_r($langs);
195         //print_R($lbits);
196         if (!isset($langs->codes[strtolower($lbits[0])])) {
197             $this->jerr('invalid lang');
198         }
199         if (!empty($lbits[1]) &&  !isset($countries->codes[$lbits[1]])) {  
200             $this->jerr('invalid lang Country component');
201             
202         }
203         return $lbits;
204     }
205     
206     function get($s)
207     {
208         if (empty($s)) {
209             die('no type');
210         }
211         
212         $lbits = $this->findLang();
213          
214         
215         
216         
217         switch($s) {
218             case 'Lang': 
219                 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
220                 break;
221
222             case 'Country':
223                 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
224                 break;
225                 
226              case 'Currency':
227                 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
228                 break;
229               
230             case 'BuildDB':
231             // by admin only?!?
232                 //DB_DataObject::debugLevel(1);
233                 $this->buildDb('l');
234                 $this->buildDb('c');
235                 $this->buildDb('m');
236                 die("DONE!");
237                 break;
238                   
239             default: 
240                 $this->jerr("ERROR");
241         }
242          
243         $this->jdata($ret);
244         exit;
245         
246     }
247     function loadFilter($type)
248     {
249         // this code only applies to Clipping module
250         if (!$this->authUser) {
251             return false;
252         }
253         
254         // this needs moving to it's own project
255         
256         if (!$this->hasModule('Clipping')) {
257             return false;
258         }
259         if ($type == 'm') {
260             return false;
261         }
262         
263         //DB_DataObject::debugLevel(1);
264         $q = DB_DataObject::factory('Projects');
265         
266         $c = DB_Dataobject::factory('Companies');
267         $c->get($this->authUser->company_id);
268         if ($c->comptype !='OWNER') {
269             $q->client_id = $this->authUser->company_id;
270         }
271         $q->selectAdd();
272         $col = ($type == 'l' ? 'languages' : 'countries');
273         $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
274         $q->whereAdd("LENGTH($col) > 0");
275         $q->find();
276         $ret = array();
277         $ret['**'] = 1;
278         while ($q->fetch()) {
279             $bits = explode(',', $q->dval);
280             foreach($bits as $k) {
281                 $ret[$k] = true;
282             }
283         }
284         return array_keys($ret);
285         
286     }
287    
288      
289     function translateList($au, $type, $k)  
290     {
291         $ar = explode(',', $k);
292         $ret = array();
293         foreach($ar as $kk) {
294             $ret[] = $this->translate($au, $type, $kk);
295         }
296         return implode(', ', $ret);
297     }
298      /**
299      * translate
300      * usage :
301      * require_once 'Pman/I18N.php';
302      * $x = new Pman_I18N();
303      * $x->translate($this->authuser, 'c', 'US');
304      * @param au - auth User
305      * @param type = 'c' or 'l'
306      * @param k - key to translate
307      * 
308      */
309      
310     function translate($au, $type, $k) 
311     {
312       
313         static $cache;
314         if (empty($k)) {
315             return '??';
316         }
317         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
318         $lbits = explode('_', strtoupper($lang));
319         $lang = $lbits[0];
320         
321         if (!isset($cache[$lang])) {
322             require_once 'I18Nv2/Country.php';
323             require_once 'I18Nv2/Language.php';
324             require_once 'I18Nv2/Currency.php';
325             $cache[$lang] = array(
326                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
327                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
328                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
329             );
330             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
331         }
332         if ($k == '**') {
333             return 'Other / Unknown';
334         }
335     
336         
337         if ($type == 'l') {
338             $tolang = explode('_', $k);
339          
340             $ret = $cache[$lang][$type]->getName($tolang[0]);
341             if (count($tolang) > 1) {
342                 $ret.= '('.$tolang[1].')'; 
343             }
344             return $ret;
345         }
346         $ret = $cache[$lang][$type]->getName($k);
347         //print_r(array($k, $ret));
348         return $ret;
349         
350         
351     }
352     
353     
354     
355     function buildDB($ltype= false, $inlang= false )
356     {
357         if ($ltype === false) {
358             
359             die("OOPS NO LTYPE");
360         }
361         if ($inlang == '**') {
362             return; // dont bother building generic..
363         }
364         if ($inlang === false) {
365             foreach( $this->cfg['l'] as $l) {
366                 $this->buildDB($ltype, $l);
367             }
368             return;
369         }
370         
371         $list =  $this->getDefaultCfg($ltype);
372         
373         DB_DataObject::debugLevel(1);
374         
375         foreach($list as $lkey) {
376             $x = DB_DataObject::factory('i18n');
377             $x->ltype = $ltype;
378             $x->lkey = $lkey;
379             $x->inlang= $inlang;
380             if ($x->find(true)) {
381                 $xx= clone($x);
382                 $x->lval = $this->translate($inlang, $ltype, $lkey);
383                 $x->update($xx);
384                 continue;
385             }
386             $x->lval = $this->translate($inlang, $ltype, $lkey);
387             $x->insert();
388             
389         }
390         
391         
392         
393         
394     }
395     
396 }