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