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