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      
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         $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
87         
88         /// verify the selected language..
89         $i = DB_DataObject::Factory('I18n');
90         $i->ltype = 'l';                           // string(1)  not_null multiple_key
91         $i->lkey = $lang;                            // string(8)  not_null
92         if (!$i->count()) {
93             $this->jerr('invalid lang configured: ' . $lang);
94         }
95         
96         
97         return explode('_', $lang);
98     }
99      
100     function get($s ='')
101     {
102      
103      
104         $lbits = $this->guessUsersLanguage();
105          
106         
107         $i = DB_DataObject::Factory('I18n');
108         
109         switch($s) {
110             case 'Lang':
111                 $i->ltype = 'l';
112                 $i->applyFilters($_REQUEST, $this->authUser, $this);
113                 $this->jdata($i->toTransList('l',  $lbits[0]));
114                 break;
115
116             case 'Country':
117                 $i->ltype = 'c';
118                 $i->applyFilters($_REQUEST, $this->authUser, $this);
119                 $this->jdata($i->toTransList('l',  $lbits[0]));
120                 
121                 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
122                 break;
123                 
124              case 'Currency':
125                 $i->ltype = 'm';
126                 $i->applyFilters($_REQUEST, $this->authUser, $this);
127                 $this->jdata($i->toTransList('l',  $lbits[0]));
128                 break;
129              
130                 
131         }
132         
133         $i = DB_DataObject::Factory('I18n');
134         $i->buildDb();
135       
136        
137         $i = DB_DataObject::Factory('I18n');
138         $cfg = $i->cfg();
139         $langs = $cfg['t'];
140        // var_dump($langs);exit;
141         $ar = array();
142         foreach($langs as $lang)
143         {
144             $rlang = array_shift(explode('_', strtoupper($lang)));
145             
146             $ar[$lang] = array();
147             $i = DB_DataObject::Factory('I18n');
148             $ar[$lang]['l'] = $i->toTransList('l',  $rlang);
149             $i = DB_DataObject::Factory('I18n');
150             $ar[$lang]['c'] =  $i->toTransList('c', $rlang);
151             $i = DB_DataObject::Factory('I18n');
152             $ar[$lang]['m'] = $i->toTransList('m', $rlang);
153         }
154         //echo '<PRE>';print_r($ar);
155         header('Content-type: text/javascript');
156         echo 'Pman.I18n.Data = ' .  json_encode($ar);
157         exit;
158         
159         
160         
161     }
162     
163  
164     
165      /**
166      * translate (used by database building);
167      * usage :
168      * require_once 'Pman/Core/I18N.php';
169      * $x = new Pman_Core_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         
186         // does it need caching?
187         
188         $i = DB_DataObject::Factory('I18n');
189         return $i->translate($lang,$type,$k);
190         
191         
192          
193         
194         
195     }
196      
197     
198 }