4 * Either we load this as a standard array at start??
7 * We basically load up all supported languages at the start of the application.
9 * By default it returns
14 l : [ { code : 'en', title : 'English' }, .... ],
15 c : [ { code : 'UK', title : 'United Kingdom' }, .... ],
16 m : [ { code : 'USD', title : 'US Dollars' }, .... ],
23 * index.php/Pman/I18N/BuildDB -- buildes the database..
24 * .. other formats are depreciated, but are supported by the old code....
27 * Database language translation should be done using the database table.
28 * So sorting can be done correctly..
30 * Configuration in index.php..
32 * 'Pman_Core_I18N' => array(
34 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
42 'c' => '*', // eg. all languages..
43 'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
48 require_once 'Pman.php';
50 class Pman_Core_I18n extends Pman
61 parent::getAuth(); // load company!
63 $au = $this->getAuthUser();
65 // $this->jerr("Not authenticated", array('authFailure' => true));
67 $this->authUser = $au;
69 $ff= HTML_FlexyFramework::get();
72 $opts = empty($ff->Pman_Core_I18N) ?
73 (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)
74 : $ff->Pman_Core_I18N;
84 function guessUsersLanguage() {
86 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
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
93 $i = DB_DataObject::Factory('I18n');
96 $i = DB_DataObject::Factory('I18n');
97 $i->ltype = 'l'; // string(1) not_null multiple_key
100 $this->jerr('invalid lang configured: ' . $lang);
105 return explode('_', $lang);
112 $lbits = $this->guessUsersLanguage();
114 if ($this->authUser && !empty($_REQUEST['debug'])) {
115 DB_DataObject::debugLevel(1);
120 $i = DB_DataObject::Factory('I18n');
127 $i->applyFilters($_REQUEST, $this->authUser, $this);
128 $this->jdata($i->toTransList('l', implode('_',$lbits)));
133 $i->applyFilters($_REQUEST, $this->authUser, $this);
134 $this->jdata($i->toTransList('c', implode('_',$lbits)));
140 $i->applyFilters($_REQUEST, $this->authUser, $this);
141 $this->jdata($i->toTransList('m', implode('_',$lbits)));
145 $ar = DateTimeZone::listAbbreviations();
148 foreach($ar as $tl => $sar) {
149 foreach($sar as $tz) {
150 $tza[] = $tz['timezone_id'];
154 $tza= array_unique($tza);
156 foreach($tza as $tz) {
158 if (empty($_REQUEST['q']) ||
160 substr($tz,0, strlen($_REQUEST['q'])),
163 $ret[] = array('tz' => $tz);
174 if (!empty($_REQUEST['debug'])) {
175 DB_DataObject::debugLevel(1);
178 $i = DB_DataObject::Factory('I18n');
182 $i = DB_DataObject::Factory('I18n');
185 // var_dump($langs);exit;
187 foreach($langs as $lang)
189 //$rlang = array_shift(explode('_', strtoupper($lang)));
190 $rlang = array_shift(explode('_', $lang));
192 $ar[$lang] = array();
193 $i = DB_DataObject::Factory('I18n');
194 $ar[$lang]['l'] = $i->toTransList('l', $rlang);
195 $i = DB_DataObject::Factory('I18n');
196 $ar[$lang]['c'] = $i->toTransList('c', $rlang);
197 $i = DB_DataObject::Factory('I18n');
198 $ar[$lang]['m'] = $i->toTransList('m', $rlang);
200 //echo '<PRE>';print_r($ar);
201 header('Content-type: text/javascript');
202 echo 'Pman.I18n.Data = ' . json_encode($ar);
212 * translate (used by database building);
214 * require_once 'Pman/Core/I18n.php';
215 * $x = new Pman_Core_I18N();
216 * $x->translate($this->authuser, 'c', 'US');
217 * @param au - auth User
218 * @param type = 'c' or 'l'
219 * @param k - key to translate
223 function translate($au, $type, $k)
230 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
232 // does it need caching?
234 $i = DB_DataObject::Factory('I18n');
235 return $i->translate($lang,$type,$k);
243 * translate a list of items
244 * @param Pman_Core_DataObjects_Person $au Authenticated user
245 * @param String $type c/l/m
246 * @param String $k 'comma' seperated list of keys to translate
249 function translateList($au, $type, $k)
251 $ar = explode(',', $k);
253 foreach($ar as $kk) {
254 $ret[] = $this->translate($au, $type, $kk);
256 return implode(', ', $ret);
260 * usage : $i = new Pman_Core_I18n();
261 * $ret = $i->convertCurrency(100,"HKD","USD");
262 * if ($ret == false) {
263 /// something went wrong.
266 * @param Pman_Core_DataObjects_Person $au Authenticated user
267 * @param String $type c/l/m
268 * @param String $k 'comma' seperated list of keys to translate
272 function convertCurrency($val, $from, $to)
274 $r = $this->loadRates();
278 if (!isset($this->rates[$from]) || !isset($this->rates[$to]) ) {
281 //echo '<PRE>';print_R($this->rates);
282 $base = (1.0 / $this->rates[$from]) * $val;
284 return $this->rates[$to] * $base;
287 var $rates = array();
290 if (!empty($this->rates)) {
293 $target = ini_get('session.save_path').'/eurofxref-daily.xml';
294 if (!file_exists($target) || filemtime($target) < (time() - 60*60*24)) {
296 $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
298 $target = dirname(__FILE__).'/eurofxref-daily.xml';
302 file_put_contents($target,$f);
305 $dom = simplexml_load_file($target);
306 $this->rates['EUR'] = 1.0;
307 $this->rates['TWD'] = 46.7008412;
308 $this->rates['VND'] = 26405.3;
310 foreach($dom->Cube->Cube->Cube as $c) {
311 //echo '<PRE>';print_r($c );
312 $this->rates[(string)$c['currency']] = (string)$c['rate'];
314 $this->rates['RMB'] = $this->rates['CNY'] ;