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