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