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