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       en : [ { code : 'end', title : 'English' }, .... ],
13       fr : ....
14    }
15  * 
16  * 
17  * other usage:
18  * 
19  * index.php/Pman/I18N/BuildDB -- buildes the database..
20  * .. other formats are depreciated, but are supported by the old code....
21  * 
22  * 
23  * Database language translation should be done using the database table.
24  * So sorting can be done correctly..
25  * 
26  * Configuration in index.php..
27  * 
28  *  'Pman_Core_I18N' => array(
29       'l' => array(
30             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
31             'id', // indonesian
32             'tl', // tagalog
33             'vi', //vietnamise
34             'hi', // hindi
35             'ta', // tamil
36             '**', // other
37         ), 
38        'c' => '*', // eg. all languages..
39        'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
40     ), 
41
42  * 
43  */
44 require_once 'Pman.php';
45
46 class Pman_Core_i18N extends Pman
47 {
48  
49     
50     // these are the default languages we support.
51     var $cfg = array(
52         'l' => array(
53             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
54             'id', // indonesian
55             'tl', // tagalog
56             'vi', //vietnamise
57             'hi', // hindi
58             'ta', // tamil
59             '**', // other
60         ),
61         'c' => array(
62              'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
63         ),
64         'm' => array(
65             'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
66         )
67     );
68     
69     
70      
71     
72     
73     function getAuth()
74     {
75         parent::getAuth(); // load company!
76         //return true;
77         $au = $this->getAuthUser();
78         //if (!$au) {
79         //    $this->jerr("Not authenticated", array('authFailure' => true));
80         //}
81         $this->authUser = $au;
82         
83         $opts = PEAR::getStaticProperty('Pman_Core_I18N', 'options');
84         if (empty($opts)) {
85             $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
86         }
87         $opts = empty($opts)  ?  array() : $opts;
88         
89         // load the cofiguration
90         foreach($opts as $k=>$v) {
91             
92             if ($v == '*') {
93                 $this->cfg[$k] = $this->getDefaultCfg($k);
94                 continue;
95             }
96             $this->cfg[$k] = is_array($v) ? $v  : explode(',', $v);
97         }
98         
99         
100         
101         
102         return true;
103     }
104     // returns a list of all countries..
105     function getDefaultCfg($t) {
106         $ret = array();
107         switch ($t) {
108             case 'c':
109                 require_once 'I18Nv2/Country.php';
110                 
111                 $c = new I18Nv2_Country('en');
112                 $ret =  array_keys($c->codes);
113                 $ret[] = '**';
114                 break;
115             case 'l':
116                 require_once 'I18Nv2/Language.php';
117                 $c = new I18Nv2_Language('en');
118                 $ret =  array_keys($c->codes);
119                 $ret[] = '**';
120                 break;
121             case 'm':
122                 require_once 'I18Nv2/Currency.php';
123                 $c = new I18Nv2_Currency('en');
124                 $ret =  array_keys($c->codes);
125                 $ret[] = '**';
126                 break;
127         }
128         foreach ($ret as $k=>$v) {
129             $ret[$k] = strtoupper($v);
130         }
131         
132         
133         return $ret;
134     }
135     
136      
137     function get($s ='')
138     {
139         
140         
141         switch ($s)
142         {
143             
144             case 'BuildDB':
145             // by admin only?!?
146                 //DB_DataObject::debugLevel(1);
147                 $this->buildDb('l');
148                 $this->buildDb('c');
149                 $this->buildDb('m');
150                 die("DONE!");
151                 break;
152                   
153             default: 
154                 // output javascript..
155                 $this->jerr("ERROR");
156         }
157          
158         $this->jdata($ret);
159         exit;
160         
161     }
162     
163     
164     
165      /**
166      * translate
167      * usage :
168      * require_once 'Pman/I18N.php';
169      * $x = new Pman_I18N();
170      * $x->translate($this->authuser, 'c', 'US');
171      * @param au - auth User
172      * @param type = 'c' or 'l'
173      * @param k - key to translate
174      * 
175      */
176      
177     function translate($au, $type, $k) 
178     {
179       
180         static $cache;
181         if (empty($k)) {
182             return '??';
183         }
184         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
185         $lbits = explode('_', strtoupper($lang));
186         $lang = $lbits[0];
187         
188         if (!isset($cache[$lang])) {
189             require_once 'I18Nv2/Country.php';
190             require_once 'I18Nv2/Language.php';
191             require_once 'I18Nv2/Currency.php';
192             $cache[$lang] = array(
193                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
194                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
195                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
196             );
197             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
198         }
199         if ($k == '**') {
200             return 'Other / Unknown';
201         }
202     
203         
204         if ($type == 'l') {
205             $tolang = explode('_', $k);
206          
207             $ret = $cache[$lang][$type]->getName($tolang[0]);
208             if (count($tolang) > 1) {
209                 $ret.= '('.$tolang[1].')'; 
210             }
211             return $ret;
212         }
213         $ret = $cache[$lang][$type]->getName($k);
214         //print_r(array($k, $ret));
215         return $ret;
216         
217         
218     }
219     
220     
221     
222     function buildDB($ltype= false, $inlang= false )
223     {
224         if ($ltype === false) {
225             
226             die("OOPS NO LTYPE");
227         }
228         if ($inlang == '**') {
229             return; // dont bother building generic..
230         }
231         if ($inlang === false) {
232             foreach( $this->cfg['l'] as $l) {
233                 $this->buildDB($ltype, $l);
234             }
235             return;
236         }
237         
238         $list =  $this->getDefaultCfg($ltype);
239         
240         DB_DataObject::debugLevel(1);
241         
242         foreach($list as $lkey) {
243             $x = DB_DataObject::factory('i18n');
244             $x->ltype = $ltype;
245             $x->lkey = $lkey;
246             $x->inlang= $inlang;
247             if ($x->find(true)) {
248                 $xx= clone($x);
249                 $x->lval = $this->translate($inlang, $ltype, $lkey);
250                 $x->update($xx);
251                 continue;
252             }
253             $x->lval = $this->translate($inlang, $ltype, $lkey);
254             $x->insert();
255             
256         }
257         
258         
259         
260         
261     }
262     
263 }