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