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         $ff= HTML_FlexyFramework::get();
97         $opts = empty($ff->Pman_Core_I18N) ? (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)  : $ff->Pman_Core_I18N;
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         
139         foreach ($ret as $k=>$v) {
140             $ret[$k] = strtoupper($v);
141         }
142         
143         
144         return $ret;
145     }
146     
147      
148     function get($s ='')
149     {
150         
151         
152         switch ($s)
153         {
154             
155             case 'BuildDB':
156             // by admin only?!?
157                 //DB_DataObject::debugLevel(1);
158                 $this->buildDb('l');
159                 $this->buildDb('c');
160                 $this->buildDb('m');
161                 die("DONE!");
162                 break;
163                   
164             default: 
165                 $this->outputJavascript();
166                 // output javascript..
167                 $this->jerr("ERROR");
168         }
169          
170         $this->jdata($ret);
171         exit;
172         
173     }
174     
175     function outputJavascript()
176     {
177         
178         require_once 'I18Nv2/Country.php';
179         require_once 'I18Nv2/Language.php';
180         require_once 'I18Nv2/Currency.php';
181         
182         $langs = $this->cfg['t'];
183        // var_dump($langs);exit;
184         $ar = array();
185         foreach($langs as $lang)
186         {
187             $rlang = array_shift(explode('_', strtoupper($lang)));
188             
189             $ar[$lang] = array(
190                 'l' => $this->objToList('l', new I18Nv2_Language($rlang, 'UTF-8')),
191                 'c' => $this->objToList('c', new I18Nv2_Country($rlang, 'UTF-8')),
192                 'm' => $this->objToList('m', new I18Nv2_Currency($rlang, 'UTF-8'))
193             );
194         }
195         //echo '<PRE>';print_r($ar);
196         header('Content-type: text/javascript');
197         echo 'Pman.I18n.Data = ' .  json_encode($ar);
198         exit;
199         
200         
201         
202     }
203     function objToList($type, $obj) {
204         $ret = array();
205         echo '<PRE>';print_r($this->cfg);
206         
207          
208         foreach($this->cfg[$type] as $k) {
209             $sub = false;
210             if (strpos($k, '_') !== false) {
211                 $bits = explode('_', $k);
212                 $k = array_shift($bits);
213                 $sub = array_shift($bits);
214             }
215             $v = $k == '**' ? 'Other' : $obj->getName($k);
216             
217             if ($sub) {
218                 $v .= ' ('.$sub.')';
219             }
220             
221             $ret[] = array(
222                 'code'=>   $type=='l' ? strtolower($k) : strtoupper($k), 
223                 'title' => $v
224             );
225         }
226         return $ret;
227     }
228     
229      /**
230      * translate (used by database building);
231      * usage :
232      * require_once 'Pman/Core/I18N.php';
233      * $x = new Pman_Core_I18N();
234      * $x->translate($this->authuser, 'c', 'US');
235      * @param au - auth User
236      * @param type = 'c' or 'l'
237      * @param k - key to translate
238      * 
239      */
240      
241     function translate($au, $type, $k) 
242     {
243       
244         static $cache;
245         if (empty($k)) {
246             return '??';
247         }
248         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
249         $lbits = explode('_', strtoupper($lang));
250         $lang = $lbits[0];
251         
252         if (!isset($cache[$lang])) {
253             require_once 'I18Nv2/Country.php';
254             require_once 'I18Nv2/Language.php';
255             require_once 'I18Nv2/Currency.php';
256             $cache[$lang] = array(
257                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
258                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
259                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
260             );
261             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
262         }
263         if ($k == '**') {
264             return 'Other / Unknown';
265         }
266     
267         
268         if ($type == 'l') {
269             $tolang = explode('_', $k);
270          
271             $ret = $cache[$lang][$type]->getName($tolang[0]);
272             if (count($tolang) > 1) {
273                 $ret.= '('.$tolang[1].')'; 
274             }
275             return $ret;
276         }
277         $ret = $cache[$lang][$type]->getName($k);
278         //print_r(array($k, $ret));
279         return $ret;
280         
281         
282     }
283     
284     
285     
286     function buildDB($ltype= false, $inlang= false )
287     {
288         if ($ltype === false) {
289             
290             die("OOPS NO LTYPE");
291         }
292         if ($inlang == '**') {
293             return; // dont bother building generic..
294         }
295         if ($inlang === false) {
296             foreach( $this->cfg['t'] as $l) {
297                 $this->buildDB($ltype, $l);
298             }
299             return;
300         }
301         
302         $list =  $this->getDefaultCfg($ltype);
303         
304         DB_DataObject::debugLevel(1);
305         
306         foreach($list as $lkey) {
307             $x = DB_DataObject::factory('i18n');
308             $x->ltype = $ltype;
309             $x->lkey = $lkey;
310             $x->inlang= $inlang;
311             if ($x->find(true)) {
312                 $xx= clone($x);
313                 $x->lval = $this->translate($inlang, $ltype, $lkey);
314                 $x->update($xx);
315                 continue;
316             }
317             $x->lval = $this->translate($inlang, $ltype, $lkey);
318             $x->insert();
319             
320         }
321         
322         
323         
324         
325     }
326     
327 }