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       
13       en : {
14           l :  [ { code : 'en', title : 'English' }, .... ],
15           c :  [ { code : 'UK', title : 'United Kingdom' }, .... ],
16           m :  [ { code : 'USD', title : 'US Dollars' }, .... ],
17       fr : ....
18    }
19  * 
20  * 
21  * other usage:
22  * 
23  * index.php/Pman/I18N/BuildDB -- buildes the database..
24  * .. other formats are depreciated, but are supported by the old code....
25  * 
26  * 
27  * Database language translation should be done using the database table.
28  * So sorting can be done correctly..
29  * 
30  * Configuration in index.php..
31  * 
32  *  'Pman_Core_I18N' => array(
33       'l' => array(
34             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
35             'id', // indonesian
36             'tl', // tagalog
37             'vi', //vietnamise
38             'hi', // hindi
39             'ta', // tamil
40             '**', // other
41         ), 
42        'c' => '*', // eg. all languages..
43        'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
44     ), 
45
46  * 
47  */
48 require_once 'Pman.php';
49
50 class Pman_Core_i18N extends Pman
51 {
52  
53     
54     // these are the default languages we support.
55     // they will allways be overlaid with the current configuration (via getAuth)
56     var $cfg = array(
57         // translated versions availalable
58         't' => array(
59             'en', 'zh_CN',   'zh_HK', 
60         ),
61         // languages available
62         'l' => array(
63             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
64             'id', // indonesian
65             'tl', // tagalog
66             'vi', //vietnamise
67             'hi', // hindi
68             'ta', // tamil
69             '**', // other
70         ),
71         'c' => array(
72              'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
73         ),
74         'm' => array(
75             'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
76         )
77     );
78     
79     
80      
81     
82     
83     function getAuth()
84     {
85         parent::getAuth(); // load company!
86         //return true;
87         $au = $this->getAuthUser();
88         //if (!$au) {
89         //    $this->jerr("Not authenticated", array('authFailure' => true));
90         //}
91         $this->authUser = $au;
92         
93         $opts = PEAR::getStaticProperty('Pman_Core_I18N', 'options');
94         if (empty($opts)) {
95             $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
96         }
97         $opts = empty($opts)  ?  array() : $opts;
98         
99         // load the cofiguration
100         foreach($opts as $k=>$v) {
101             
102             if ($v == '*') {
103                 $this->cfg[$k] = $this->getDefaultCfg($k);
104                 continue;
105             }
106             $this->cfg[$k] = is_array($v) ? $v  : explode(',', $v);
107         }
108         
109         
110         
111         
112         return true;
113     }
114     // returns a list of all countries..
115     function getDefaultCfg($t) {
116         $ret = array();
117         switch ($t) {
118             case 'c':
119                 require_once 'I18Nv2/Country.php';
120                 
121                 $c = new I18Nv2_Country('en');
122                 $ret =  array_keys($c->codes);
123                 $ret[] = '**';
124                 break;
125             case 'l':
126                 require_once 'I18Nv2/Language.php';
127                 $c = new I18Nv2_Language('en');
128                 $ret =  array_keys($c->codes);
129                 $ret[] = '**';
130                 break;
131             case 'm':
132                 require_once 'I18Nv2/Currency.php';
133                 $c = new I18Nv2_Currency('en');
134                 $ret =  array_keys($c->codes);
135                 $ret[] = '**';
136                 break;
137         }
138         foreach ($ret as $k=>$v) {
139             $ret[$k] = strtoupper($v);
140         }
141         
142         
143         return $ret;
144     }
145     
146      
147     function get($s ='')
148     {
149         
150         
151         switch ($s)
152         {
153             
154             case 'BuildDB':
155             // by admin only?!?
156                 //DB_DataObject::debugLevel(1);
157                 $this->buildDb('l');
158                 $this->buildDb('c');
159                 $this->buildDb('m');
160                 die("DONE!");
161                 break;
162                   
163             default: 
164                 $this->outputJavascript();
165                 // output javascript..
166                 $this->jerr("ERROR");
167         }
168          
169         $this->jdata($ret);
170         exit;
171         
172     }
173     
174     function outputJavascript()
175     {
176         
177         require_once 'I18Nv2/Country.php';
178         require_once 'I18Nv2/Language.php';
179         require_once 'I18Nv2/Currency.php';
180         
181         $langs = $this->cfg['t'];
182        // var_dump($langs);exit;
183         $ar = array();
184         foreach($langs as $lang)
185         {
186             $lang = array_shift(explode('_', strtoupper($lang)));
187             
188             $ar[$lang] = array(
189                 'l' => $this->objToList(new I18Nv2_Language($lang, 'UTF-8')),
190                 'c' => $this->objToList(new I18Nv2_Country($lang, 'UTF-8')),
191                 'm' => $this->objToList(new I18Nv2_Currency($lang, 'UTF-8'))
192             );
193         }
194         header('Content-type: text/javascript');
195         echo 'Pman.I18n.Data = ' .  json_encode($ar);
196         exit;
197         
198         
199         
200     }
201     function objToList($obj) {
202         $ret = array();
203         foreach($obj->codes as $k=>$v) {
204             
205             $ret[] = array(
206                 'code'=> $k , 
207                 'title' => $v
208             );
209         }
210         return $ret;
211     }
212     
213      /**
214      * translate (used by database building);
215      * usage :
216      * require_once 'Pman/Core/I18N.php';
217      * $x = new Pman_Core_I18N();
218      * $x->translate($this->authuser, 'c', 'US');
219      * @param au - auth User
220      * @param type = 'c' or 'l'
221      * @param k - key to translate
222      * 
223      */
224      
225     function translate($au, $type, $k) 
226     {
227       
228         static $cache;
229         if (empty($k)) {
230             return '??';
231         }
232         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
233         $lbits = explode('_', strtoupper($lang));
234         $lang = $lbits[0];
235         
236         if (!isset($cache[$lang])) {
237             require_once 'I18Nv2/Country.php';
238             require_once 'I18Nv2/Language.php';
239             require_once 'I18Nv2/Currency.php';
240             $cache[$lang] = array(
241                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
242                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
243                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
244             );
245             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
246         }
247         if ($k == '**') {
248             return 'Other / Unknown';
249         }
250     
251         
252         if ($type == 'l') {
253             $tolang = explode('_', $k);
254          
255             $ret = $cache[$lang][$type]->getName($tolang[0]);
256             if (count($tolang) > 1) {
257                 $ret.= '('.$tolang[1].')'; 
258             }
259             return $ret;
260         }
261         $ret = $cache[$lang][$type]->getName($k);
262         //print_r(array($k, $ret));
263         return $ret;
264         
265         
266     }
267     
268     
269     
270     function buildDB($ltype= false, $inlang= false )
271     {
272         if ($ltype === false) {
273             
274             die("OOPS NO LTYPE");
275         }
276         if ($inlang == '**') {
277             return; // dont bother building generic..
278         }
279         if ($inlang === false) {
280             foreach( $this->cfg['t'] as $l) {
281                 $this->buildDB($ltype, $l);
282             }
283             return;
284         }
285         
286         $list =  $this->getDefaultCfg($ltype);
287         
288         DB_DataObject::debugLevel(1);
289         
290         foreach($list as $lkey) {
291             $x = DB_DataObject::factory('i18n');
292             $x->ltype = $ltype;
293             $x->lkey = $lkey;
294             $x->inlang= $inlang;
295             if ($x->find(true)) {
296                 $xx= clone($x);
297                 $x->lval = $this->translate($inlang, $ltype, $lkey);
298                 $x->update($xx);
299                 continue;
300             }
301             $x->lval = $this->translate($inlang, $ltype, $lkey);
302             $x->insert();
303             
304         }
305         
306         
307         
308         
309     }
310     
311 }