fix #8131 - chinese translations
[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      
55     
56      
57     
58     
59     function getAuth()
60     {
61         parent::getAuth(); // load company!
62         //return true;
63         $au = $this->getAuthUser();
64         //if (!$au) {
65         //    $this->jerr("Not authenticated", array('authFailure' => true));
66         //}
67         $this->authUser = $au;
68         
69         $ff= HTML_FlexyFramework::get();
70          
71         
72         $opts = empty($ff->Pman_Core_I18N) ?
73                 (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)
74                 : $ff->Pman_Core_I18N;
75         
76          
77         
78         
79         
80         return true;
81     }
82      
83     
84     function guessUsersLanguage()
85     {
86         $this->sessionState(0);
87         
88         $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
89          /// verify the selected language..
90         $i = DB_DataObject::Factory('I18n');
91         $i->ltype = 'l';                           // string(1)  not_null multiple_key
92         $i->lkey = $lang;                            // string(8)  not_null
93         if (!$i->count()) {    
94             $i = DB_DataObject::Factory('I18n');
95             $i->buildDb();
96             
97             $i = DB_DataObject::Factory('I18n');
98             $i->ltype = 'l';                           // string(1)  not_null multiple_key
99             $i->lkey = $lang;  
100             if (!$i->count()) { 
101                 $this->jerr('invalid lang configured: ' . $lang);
102             }
103         }
104         
105         
106         return explode('_', $lang);
107     }
108      
109     function get($s ='', $opts=array())
110     {
111      
112         $this->sessionState(0);
113         $lbits = $this->guessUsersLanguage();
114          
115         if ($this->authUser && !empty($_REQUEST['_debug'])) {
116             DB_DataObject::debugLevel(1);
117         }
118         
119         
120         
121         $i = DB_DataObject::Factory('I18n');
122         $i->is_active = 1;
123         switch($s) {
124             case 'Lang':
125                  
126                 
127                 $i->ltype = 'l';
128                 $i->applyFilters($_REQUEST, $this->authUser, $this);
129                 $this->jdata($i->toTransList('l',  implode('_',$lbits)));
130                 break;
131
132             case 'Country':
133                 $i->ltype = 'c';
134                 $i->applyFilters($_REQUEST, $this->authUser, $this);
135                 $this->jdata($i->toTransList('c',  implode('_',$lbits)));
136                
137                 break;
138                 
139             case 'Currency':
140                 $i->ltype = 'm';
141                 $i->applyFilters($_REQUEST, $this->authUser, $this);
142                 $this->jdata($i->toTransList('m',  implode('_',$lbits)));
143                 break;
144             
145             case 'Timezone':
146                 $ar = DateTimeZone::listAbbreviations();
147                 $ret = array();
148                 $tza = array();
149                 foreach($ar as $tl => $sar) {
150                     foreach($sar as $tz) {
151                         $tza[]  = $tz['timezone_id'];
152                     
153                     }
154                 }
155                 $tza= array_unique($tza);
156                 sort($tza);
157                 foreach($tza as $tz) {
158                     //filtering..
159                     if (empty($_REQUEST['q']) ||
160                             0 === strcasecmp(
161                                     substr($tz,0, strlen($_REQUEST['q'])),
162                                     $_REQUEST['q'])
163                     ) {
164                         $ret[] = array('tz' => $tz);
165                     }
166                     
167                 }
168                 $this->jdata($ret);
169                 
170                 
171                 
172              
173                 
174         }
175         if (!empty($_REQUEST['debug'])) {
176             DB_DataObject::debugLevel(1);
177         }
178         
179         $i = DB_DataObject::Factory('I18n');
180         $i->buildDB();
181       
182        
183         $i = DB_DataObject::Factory('I18n');
184         $cfg = $i->cfg();
185         $langs = $cfg['t'];
186        // var_dump($langs);exit;
187         $ar = array();
188         foreach($langs as $lang)
189         {
190             //$rlang = array_shift(explode('_', strtoupper($lang)));
191             $ll = explode('_', $lang);
192             $rlang = array_shift($ll);
193             
194             $ar[$lang] = array();
195             $i = DB_DataObject::Factory('I18n');
196             $ar[$lang]['l'] = $i->toTransList('l',  $rlang);
197             $i = DB_DataObject::Factory('I18n');
198             $ar[$lang]['c'] =  $i->toTransList('c', $rlang);
199             $i = DB_DataObject::Factory('I18n');
200             $ar[$lang]['m'] = $i->toTransList('m', $rlang);
201         }
202         require_once 'I18Nv2/CurrencyMap.php';
203         
204         $ar['currency_map'] = I18Nv2_CurrencyMap::$s_map;
205         
206         //echo '<PRE>';print_r($ar);
207         header('Content-type: text/javascript');
208         echo "Roo.namespace('Pman.I18n');";
209         echo 'Pman.I18n.Data = ' .  json_encode($ar);
210         exit;
211         
212         
213         
214     }
215     
216  
217     
218      /**
219      * translate (used by database building);
220      * usage :
221      * require_once 'Pman/Core/I18n.php';
222      * $x = new Pman_Core_I18N();
223      * $x->translate($this->authuser, 'c', 'US');
224      * @param au - auth User
225      * @param type = 'c' or 'l'
226      * @param k - key to translate
227      * 
228      */
229      
230     function translate($au, $type, $k) 
231     {
232       
233         static $cache;
234         if (empty($k)) {
235             return '??';
236         }
237         $fo = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
238         
239         $fallback_lang = empty($fo['locale']) ? 'en' : $fo['locale'];
240             
241         
242         $lang =  empty($au ) ? $fallback_lang : (is_string($au) ? $au : $au->lang);
243         
244         // does it need caching?
245         
246         $i = DB_DataObject::Factory('I18n');
247         return $i->translate($lang,$type,$k);
248         
249         
250          
251         
252         
253     }
254     /**
255      * translate a list of items
256      * @param Pman_Core_DataObjects_Person $au Authenticated user
257      * @param String                      $type  c/l/m
258      * @param String                      $k     'comma' seperated list of keys to translate
259      */
260     
261     function translateList($au, $type, $k)  
262     {
263         $ar = explode(',', $k);
264         $ret = array();
265         foreach($ar as $kk) {
266             $ret[] = $this->translate($au, $type, $kk);
267         }
268         return implode(', ', $ret);
269     }
270     /**
271      * DO NOT USE THIS -- see core_curr_rates dataobject.
272      */
273     
274     
275     function convertCurrency($val, $from, $to)
276     {
277         
278         return DB_DAtaObject::Factory('core_curr_rate')->convert($val,$from,$to);
279     
280     }
281     /**
282      * DO NOT USE THIS -- see core_curr_rates dataobject.
283      *
284      */
285     
286     
287     
288     function loadRates()
289     {   
290         static $rates = array();
291         
292         if (!empty($rates)) {
293             $this->rates = $rates;
294             return;
295         }
296         
297         $this->rates = $rates = DB_DAtaObject::Factory('core_curr_rate')->currentRates();
298         
299     }
300     
301     
302      
303     
304 }