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             
181             $ar[$lang] = array(
182                 'l' => $this->objToList(new I18Nv2_Language($lang, 'UTF-8')),
183                 'c' => $this->objToList(new I18Nv2_Country($lang, 'UTF-8')),
184                 'm' => $this->objToList(new I18Nv2_Currency($lang, 'UTF-8'))
185             );
186         }
187         header('Content-type: text/javascript');
188         echo 'Pman.I18n.Data = ' .  json_encode($ar);
189         exit;
190         
191         
192         
193     }
194     function objToList($obj) {
195         $ret = array();
196         foreach($obj->codes as $k=>$v) {
197             
198             $ret[] = array(
199                 'code'=>$k , 
200                 'title' => $v
201             );
202         }
203         return $ret;
204     }
205     
206      /**
207      * translate (used by database building);
208      * usage :
209      * require_once 'Pman/Core/I18N.php';
210      * $x = new Pman_Core_I18N();
211      * $x->translate($this->authuser, 'c', 'US');
212      * @param au - auth User
213      * @param type = 'c' or 'l'
214      * @param k - key to translate
215      * 
216      */
217      
218     function translate($au, $type, $k) 
219     {
220       
221         static $cache;
222         if (empty($k)) {
223             return '??';
224         }
225         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
226         $lbits = explode('_', strtoupper($lang));
227         $lang = $lbits[0];
228         
229         if (!isset($cache[$lang])) {
230             require_once 'I18Nv2/Country.php';
231             require_once 'I18Nv2/Language.php';
232             require_once 'I18Nv2/Currency.php';
233             $cache[$lang] = array(
234                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
235                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
236                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
237             );
238             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
239         }
240         if ($k == '**') {
241             return 'Other / Unknown';
242         }
243     
244         
245         if ($type == 'l') {
246             $tolang = explode('_', $k);
247          
248             $ret = $cache[$lang][$type]->getName($tolang[0]);
249             if (count($tolang) > 1) {
250                 $ret.= '('.$tolang[1].')'; 
251             }
252             return $ret;
253         }
254         $ret = $cache[$lang][$type]->getName($k);
255         //print_r(array($k, $ret));
256         return $ret;
257         
258         
259     }
260     
261     
262     
263     function buildDB($ltype= false, $inlang= false )
264     {
265         if ($ltype === false) {
266             
267             die("OOPS NO LTYPE");
268         }
269         if ($inlang == '**') {
270             return; // dont bother building generic..
271         }
272         if ($inlang === false) {
273             foreach( $this->cfg['l'] as $l) {
274                 $this->buildDB($ltype, $l);
275             }
276             return;
277         }
278         
279         $list =  $this->getDefaultCfg($ltype);
280         
281         DB_DataObject::debugLevel(1);
282         
283         foreach($list as $lkey) {
284             $x = DB_DataObject::factory('i18n');
285             $x->ltype = $ltype;
286             $x->lkey = $lkey;
287             $x->inlang= $inlang;
288             if ($x->find(true)) {
289                 $xx= clone($x);
290                 $x->lval = $this->translate($inlang, $ltype, $lkey);
291                 $x->update($xx);
292                 continue;
293             }
294             $x->lval = $this->translate($inlang, $ltype, $lkey);
295             $x->insert();
296             
297         }
298         
299         
300         
301         
302     }
303     
304 }