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