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