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             case 'BuildDB':
144             // by admin only?!?
145                 //DB_DataObject::debugLevel(1);
146                 $this->buildDb('l');
147                 $this->buildDb('c');
148                 $this->buildDb('m');
149                 die("DONE!");
150                 break;
151                   
152             default: 
153                 // output javascript..
154                 $this->jerr("ERROR");
155         }
156          
157         $this->jdata($ret);
158         exit;
159         
160     }
161     
162     
163     
164      /**
165      * translate
166      * usage :
167      * require_once 'Pman/I18N.php';
168      * $x = new Pman_I18N();
169      * $x->translate($this->authuser, 'c', 'US');
170      * @param au - auth User
171      * @param type = 'c' or 'l'
172      * @param k - key to translate
173      * 
174      */
175      
176     function translate($au, $type, $k) 
177     {
178       
179         static $cache;
180         if (empty($k)) {
181             return '??';
182         }
183         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
184         $lbits = explode('_', strtoupper($lang));
185         $lang = $lbits[0];
186         
187         if (!isset($cache[$lang])) {
188             require_once 'I18Nv2/Country.php';
189             require_once 'I18Nv2/Language.php';
190             require_once 'I18Nv2/Currency.php';
191             $cache[$lang] = array(
192                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
193                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
194                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
195             );
196             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
197         }
198         if ($k == '**') {
199             return 'Other / Unknown';
200         }
201     
202         
203         if ($type == 'l') {
204             $tolang = explode('_', $k);
205          
206             $ret = $cache[$lang][$type]->getName($tolang[0]);
207             if (count($tolang) > 1) {
208                 $ret.= '('.$tolang[1].')'; 
209             }
210             return $ret;
211         }
212         $ret = $cache[$lang][$type]->getName($k);
213         //print_r(array($k, $ret));
214         return $ret;
215         
216         
217     }
218     
219     
220     
221     function buildDB($ltype= false, $inlang= false )
222     {
223         if ($ltype === false) {
224             
225             die("OOPS NO LTYPE");
226         }
227         if ($inlang == '**') {
228             return; // dont bother building generic..
229         }
230         if ($inlang === false) {
231             foreach( $this->cfg['l'] as $l) {
232                 $this->buildDB($ltype, $l);
233             }
234             return;
235         }
236         
237         $list =  $this->getDefaultCfg($ltype);
238         
239         DB_DataObject::debugLevel(1);
240         
241         foreach($list as $lkey) {
242             $x = DB_DataObject::factory('i18n');
243             $x->ltype = $ltype;
244             $x->lkey = $lkey;
245             $x->inlang= $inlang;
246             if ($x->find(true)) {
247                 $xx= clone($x);
248                 $x->lval = $this->translate($inlang, $ltype, $lkey);
249                 $x->update($xx);
250                 continue;
251             }
252             $x->lval = $this->translate($inlang, $ltype, $lkey);
253             $x->insert();
254             
255         }
256         
257         
258         
259         
260     }
261     
262 }