* By default it returns
*
* Pman.I18n.Data = {
- en : [ { code : 'end', title : 'English' }, .... ],
+
+ en : {
+ l : [ { code : 'en', title : 'English' }, .... ],
+ c : [ { code : 'UK', title : 'United Kingdom' }, .... ],
+ m : [ { code : 'USD', title : 'US Dollars' }, .... ],
fr : ....
}
*
* .. other formats are depreciated, but are supported by the old code....
*
*
- *
+ * Database language translation should be done using the database table.
+ * So sorting can be done correctly..
*
* Configuration in index.php..
*
- *
- *
* 'Pman_Core_I18N' => array(
'l' => array(
'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
*/
require_once 'Pman.php';
-class Pman_Core_i18N extends Pman
+class Pman_Core_I18n extends Pman
{
- // these are the default languages we support.
- var $cfg = array(
- 'l' => array(
- 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
- 'id', // indonesian
- 'tl', // tagalog
- 'vi', //vietnamise
- 'hi', // hindi
- 'ta', // tamil
- '**', // other
- ),
- 'c' => array(
- 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
- ),
- 'm' => array(
- 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
- )
- );
-
+
//}
$this->authUser = $au;
- $opts = PEAR::getStaticProperty('Pman_Core_I18N', 'options');
- if (empty($opts) {
- $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
- }
- $opts = empty($opts) ? array() : $opts;
+ $ff= HTML_FlexyFramework::get();
+
- // load the cofiguration
- foreach($opts as $k=>$v) {
-
- if ($v == '*') {
- $this->cfg[$k] = $this->getDefaultCfg($k);
- continue;
- }
- $this->cfg[$k] = is_array($v) ? $v : explode(',', $v);
- }
+ $opts = empty($ff->Pman_Core_I18N) ?
+ (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)
+ : $ff->Pman_Core_I18N;
+
return true;
}
- // returns a list of all countries..
- function getDefaultCfg($t) {
- $ret = array();
- switch ($t) {
- case 'c':
- require_once 'I18Nv2/Country.php';
-
- $c = new I18Nv2_Country('en');
- $ret = array_keys($c->codes);
- $ret[] = '**';
- break;
- case 'l':
- require_once 'I18Nv2/Language.php';
- $c = new I18Nv2_Language('en');
- $ret = array_keys($c->codes);
- $ret[] = '**';
- break;
- case 'm':
- require_once 'I18Nv2/Currency.php';
- $c = new I18Nv2_Currency('en');
- $ret = array_keys($c->codes);
- $ret[] = '**';
- break;
- }
- foreach ($ret as $k=>$v) {
- $ret[$k] = strtoupper($v);
- }
-
-
- return $ret;
- }
-
-
-
- function setSession($au)
- {
- $this->authUser = $au;
- $lbits = implode('_', $this->findLang());
- if (empty($_SESSION['Pman_I18N'])) {
- $_SESSION['Pman_I18N'] = array();
- }
-
- $_SESSION['Pman_I18N'][$lbits] = array(
- 'l' => $this->getList('l', $lbits),
- 'c' => $this->getList('c', $lbits),
- 'm' => $this->getList('m', $lbits),
- );
-
-
- }
-
- function getList($type, $inlang,$fi=false)
- {
- //$l = new I18Nv2_Language($inlang);
- //$c= new I18Nv2_Country($inlang);
- $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
- // print_r($filter);
-
- $ret = array();
-
-
-
-
- foreach($this->cfg[$type] as $k) {
- if (is_array($filter) && !in_array($k, $filter)) {
- continue;
- }
-
- $ret[] = array(
- 'code'=>$k ,
- 'title' => $this->translate($inlang, $type, $k)
- );
- continue;
-
- }
- // sort it??
- return $ret;
-
- }
- function findLang() {
+ function guessUsersLanguage() {
$lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
- $lbits = explode('_', strtoupper($lang));
- $lbits[0] = strtolower($lbits[0]);
- require_once 'I18Nv2/Country.php';
- require_once 'I18Nv2/Language.php';
- $langs = new I18Nv2_Language('en');
- $countries = new I18Nv2_Country('en');
- // print_r($langs);
- //print_R($lbits);
- if (!isset($langs->codes[strtolower($lbits[0])])) {
- $this->jerr('invalid lang');
- }
- if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
- $this->jerr('invalid lang Country component');
+
+ /// verify the selected language..
+ $i = DB_DataObject::Factory('I18n');
+ $i->ltype = 'l'; // string(1) not_null multiple_key
+ $i->lkey = $lang; // string(8) not_null
+ if (!$i->count()) {
+ $i = DB_DataObject::Factory('I18n');
+ $i->buildDb();
+ $i = DB_DataObject::Factory('I18n');
+ $i->ltype = 'l'; // string(1) not_null multiple_key
+ $i->lkey = $lang;
+ if (!$i->count()) {
+ $this->jerr('invalid lang configured: ' . $lang);
+ }
}
- return $lbits;
+
+
+ return explode('_', $lang);
}
-
- function get($s)
+
+ function get($s ='')
{
- if (empty($s)) {
- die('no type');
- }
-
- $lbits = $this->findLang();
+
+
+ $lbits = $this->guessUsersLanguage();
-
+ $i = DB_DataObject::Factory('I18n');
switch($s) {
- case 'Lang':
- $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
+ case 'Lang':
+ $i->ltype = 'l';
+ $i->applyFilters($_REQUEST, $this->authUser, $this);
+ $this->jdata($i->toTransList('l', $lbits[0]));
break;
case 'Country':
- $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
+ $i->ltype = 'c';
+ $i->applyFilters($_REQUEST, $this->authUser, $this);
+ $this->jdata($i->toTransList('c', $lbits[0]));
+
break;
case 'Currency':
- $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
- break;
-
- case 'BuildDB':
- // by admin only?!?
- //DB_DataObject::debugLevel(1);
- $this->buildDb('l');
- $this->buildDb('c');
- $this->buildDb('m');
- die("DONE!");
+ $i->ltype = 'm';
+ $i->applyFilters($_REQUEST, $this->authUser, $this);
+ $this->jdata($i->toTransList('m', $lbits[0]));
break;
-
- default:
- $this->jerr("ERROR");
- }
-
- $this->jdata($ret);
- exit;
-
- }
- function loadFilter($type)
- {
- // this code only applies to Clipping module
- if (!$this->authUser) {
- return false;
+
+
}
- // this needs moving to it's own project
-
- if (!$this->hasModule('Clipping')) {
- return false;
- }
- if ($type == 'm') {
- return false;
+ $i = DB_DataObject::Factory('I18n');
+ $i->buildDb();
+
+
+ $i = DB_DataObject::Factory('I18n');
+ $cfg = $i->cfg();
+ $langs = $cfg['t'];
+ // var_dump($langs);exit;
+ $ar = array();
+ foreach($langs as $lang)
+ {
+ $rlang = array_shift(explode('_', strtoupper($lang)));
+
+ $ar[$lang] = array();
+ $i = DB_DataObject::Factory('I18n');
+ $ar[$lang]['l'] = $i->toTransList('l', $rlang);
+ $i = DB_DataObject::Factory('I18n');
+ $ar[$lang]['c'] = $i->toTransList('c', $rlang);
+ $i = DB_DataObject::Factory('I18n');
+ $ar[$lang]['m'] = $i->toTransList('m', $rlang);
}
+ //echo '<PRE>';print_r($ar);
+ header('Content-type: text/javascript');
+ echo 'Pman.I18n.Data = ' . json_encode($ar);
+ exit;
- //DB_DataObject::debugLevel(1);
- $q = DB_DataObject::factory('Projects');
- $c = DB_Dataobject::factory('Companies');
- $c->get($this->authUser->company_id);
- if ($c->comptype !='OWNER') {
- $q->client_id = $this->authUser->company_id;
- }
- $q->selectAdd();
- $col = ($type == 'l' ? 'languages' : 'countries');
- $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
- $q->whereAdd("LENGTH($col) > 0");
- $q->find();
- $ret = array();
- $ret['**'] = 1;
- while ($q->fetch()) {
- $bits = explode(',', $q->dval);
- foreach($bits as $k) {
- $ret[$k] = true;
- }
- }
- return array_keys($ret);
}
-
-
- function translateList($au, $type, $k)
- {
- $ar = explode(',', $k);
- $ret = array();
- foreach($ar as $kk) {
- $ret[] = $this->translate($au, $type, $kk);
- }
- return implode(', ', $ret);
- }
+
+
+
/**
- * translate
+ * translate (used by database building);
* usage :
- * require_once 'Pman/I18N.php';
- * $x = new Pman_I18N();
+ * require_once 'Pman/Core/I18N.php';
+ * $x = new Pman_Core_I18N();
* $x->translate($this->authuser, 'c', 'US');
* @param au - auth User
* @param type = 'c' or 'l'
return '??';
}
$lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
- $lbits = explode('_', strtoupper($lang));
- $lang = $lbits[0];
- if (!isset($cache[$lang])) {
- require_once 'I18Nv2/Country.php';
- require_once 'I18Nv2/Language.php';
- require_once 'I18Nv2/Currency.php';
- $cache[$lang] = array(
- 'l' => new I18Nv2_Language($lang, 'UTF-8'),
- 'c' => new I18Nv2_Country($lang, 'UTF-8'),
- 'm' => new I18Nv2_Currency($lang, 'UTF-8')
- );
- //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
- }
- if ($k == '**') {
- return 'Other / Unknown';
- }
-
+ // does it need caching?
+
+ $i = DB_DataObject::Factory('I18n');
+ return $i->translate($lang,$type,$k);
+
- if ($type == 'l') {
- $tolang = explode('_', $k);
- $ret = $cache[$lang][$type]->getName($tolang[0]);
- if (count($tolang) > 1) {
- $ret.= '('.$tolang[1].')';
- }
- return $ret;
- }
- $ret = $cache[$lang][$type]->getName($k);
- //print_r(array($k, $ret));
- return $ret;
}
+ /**
+ * translate a list of items
+ * @param Pman_Core_DataObjects_Person $au Authenticated user
+ * @param String $type c/l/m
+ * @param String $k 'comma' seperated list of keys to translate
+ */
+ function translateList($au, $type, $k)
+ {
+ $ar = explode(',', $k);
+ $ret = array();
+ foreach($ar as $kk) {
+ $ret[] = $this->translate($au, $type, $kk);
+ }
+ return implode(', ', $ret);
+ }
+ /**
+ * convert rate:
+ * usage : $i = new Pman_Core_I18n();
+ * $ret = $i->convertCurrency(100,"HKD","USD");
+ * if ($ret == false) {
+ /// something went wrong.
+ }
+ *
+ * @param Pman_Core_DataObjects_Person $au Authenticated user
+ * @param String $type c/l/m
+ * @param String $k 'comma' seperated list of keys to translate
+ */
- function buildDB($ltype= false, $inlang= false )
+ function convertCurrency($val, $from, $to)
{
- if ($ltype === false) {
-
- die("OOPS NO LTYPE");
+ $r = $this->loadRates();
+ if ($r === false) {
+ return false;
}
- if ($inlang == '**') {
- return; // dont bother building generic..
+ if (!isset($this->rates[$from]) || !isset($this->rates[$to]) ) {
+ return false;
}
- if ($inlang === false) {
- foreach( $this->cfg['l'] as $l) {
- $this->buildDB($ltype, $l);
- }
- return;
+ //echo '<PRE>';print_R($this->rates);
+ $base = (1.0 / $this->rates[$from]) * $val;
+
+ return $this->rates[$to] * $base;
+
+ }
+ var $rates = array();
+ function loadRates()
+ {
+ if (!empty($this->rates)) {
+ return true;
}
-
- $list = $this->getDefaultCfg($ltype);
-
- DB_DataObject::debugLevel(1);
-
- foreach($list as $lkey) {
- $x = DB_DataObject::factory('i18n');
- $x->ltype = $ltype;
- $x->lkey = $lkey;
- $x->inlang= $inlang;
- if ($x->find(true)) {
- $xx= clone($x);
- $x->lval = $this->translate($inlang, $ltype, $lkey);
- $x->update($xx);
- continue;
+ $target = ini_get('session.save_path').'/eurofxref-daily.xml';
+ if (!file_exists($target) || filemtime($target) < (time() - 60*60*24)) {
+ $f = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
+ if (!strlen($f)) {
+ return false;
}
- $x->lval = $this->translate($inlang, $ltype, $lkey);
- $x->insert();
-
+ file_put_contents($target,$f);
+ }
+ $dom = simplexml_load_file($target);
+ $this->rates['EUR'] = 1.0;
+ $this->rates['TWD'] = 46.7008412;
+ $this->rates['VND'] = 26405.3;
+
+ foreach($dom->Cube->Cube->Cube as $c) {
+ //echo '<PRE>';print_r($c );
+ $this->rates[(string)$c['currency']] = (string)$c['rate'];
}
-
-
-
-
+ $this->rates['RMB'] = $this->rates['CNY'] ;
}
+
+
+
}
\ No newline at end of file