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     // THESE WILL ALLWAYS BE UPPERCASE!!!
57     var $cfg = array(
58         // translated versions availalable
59         
60         't' => array(
61             'en', 'zh_CN',   'zh_HK', 
62         ),
63         // languages available
64         'l' => array(
65             
66             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
67             'id', // indonesian
68             'tl', // tagalog
69             'vi', //vietnamise
70             'hi', // hindi
71             'ta', // tamil
72             '**', // other
73         ),
74         'c' => array(
75              'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
76         ),
77         'm' => array(
78             'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
79         )
80     );
81     
82     
83      
84     
85     
86     function getAuth()
87     {
88         parent::getAuth(); // load company!
89         //return true;
90         $au = $this->getAuthUser();
91         //if (!$au) {
92         //    $this->jerr("Not authenticated", array('authFailure' => true));
93         //}
94         $this->authUser = $au;
95         
96         $ff= HTML_FlexyFramework::get();
97          
98         
99         $opts = empty($ff->Pman_Core_I18N) ? (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)  : $ff->Pman_Core_I18N;
100         
101         $i = DB_DataObject::Factory('I18n');
102         // load the cofiguration
103         foreach($opts as $k=>$v) {
104             
105             if ($v == '*') { // everything..
106                 $this->cfg[$k] = $i->availableCodes($k);
107                 continue;
108             }
109             $this->cfg[$k] = is_array($v) ? $v  : explode(',', $v);
110         }
111         
112         
113         
114         
115         return true;
116     }
117      
118     
119      
120     function get($s ='')
121     {
122         
123         $i = DB_DataObject::Factory('I18n');
124         $i->buildDb();
125         $this->outputJavascript();
126     
127         
128         exit;
129         
130     }
131     
132     function outputJavascript()
133     {
134         
135         $i = DB_DataObject::Factory('I18n');
136         
137         $langs = $this->cfg['t'];
138        // var_dump($langs);exit;
139         $ar = array();
140         foreach($langs as $lang)
141         {
142             $rlang = array_shift(explode('_', strtoupper($lang)));
143             
144             $ar[$lang] = array(
145                 'l' => $i->toTransList('l',  $rlang),
146                 'c' => $i->toTransList('c', $rlang),
147                 'm' => $i->toTransList('m', $rlang),
148             );
149         }
150         //echo '<PRE>';print_r($ar);
151         header('Content-type: text/javascript');
152         echo 'Pman.I18n.Data = ' .  json_encode($ar);
153         exit;
154         
155         
156         
157     }
158     
159     function transToList($type, $tolang)
160     {
161         
162         
163         
164     }
165     
166     function objToList($type, $obj) {
167         $ret = array();
168          
169          
170         foreach($this->cfg[$type] as $k) {
171             $sub = false;
172             
173             if (strpos($k, '_') !== false) {
174                 $bits = explode('_', $k);
175                 $k = array_shift($bits);
176                 $sub = array_shift($bits);
177             }
178             $v = $k == '**' ? 'Other' : $obj->getName($k);
179             
180             if ($sub) {
181                 $v .= ' ('.$sub.')';
182             }
183             
184             $ret[] = array(
185                 'code'=>   ($type=='l' ? strtolower($k) : strtoupper($k)) . ($sub ? '_'.strtoupper($sub) : ''), 
186                 'title' => $v
187             );
188         }
189         return $ret;
190     }
191     
192      /**
193      * translate (used by database building);
194      * usage :
195      * require_once 'Pman/Core/I18N.php';
196      * $x = new Pman_Core_I18N();
197      * $x->translate($this->authuser, 'c', 'US');
198      * @param au - auth User
199      * @param type = 'c' or 'l'
200      * @param k - key to translate
201      * 
202      */
203      
204     function translate($au, $type, $k) 
205     {
206       
207         static $cache;
208         if (empty($k)) {
209             return '??';
210         }
211         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
212         $lbits = explode('_', strtoupper($lang));
213         $lang = $lbits[0];
214         
215         if (!isset($cache[$lang])) {
216             require_once 'I18Nv2/Country.php';
217             require_once 'I18Nv2/Language.php';
218             require_once 'I18Nv2/Currency.php';
219             $cache[$lang] = array(
220                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
221                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
222                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
223             );
224             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
225         }
226         if ($k == '**') {
227             return 'Other / Unknown';
228         }
229     
230         
231         if ($type == 'l') {
232             $tolang = explode('_', $k);
233          
234             $ret = $cache[$lang][$type]->getName($tolang[0]);
235             if (count($tolang) > 1) {
236                 $ret.= '('.$tolang[1].')'; 
237             }
238             return $ret;
239         }
240         $ret = $cache[$lang][$type]->getName($k);
241         //print_r(array($k, $ret));
242         return $ret;
243         
244         
245     }
246      
247     
248 }