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