3 /// provide language data!!!
4 // THIS IS PUBLICALLY ACCESSABLE..
7 * Either we load this as a standard array at start??
10 * or we treat it like a regular pulldown... - on demand...
12 * big snag is that we can not include these until the user has specified their interface language.
14 * If we do a global jS array?
15 * Pman.Lang.en = [ 'en' , 'English']
17 * If we do a standard 'store' version..
18 * { total: x, data [ { code: 'en', title: 'English' }] }
23 * index.php/Pman/I18N/BuildDB -- buildes the database..
26 * Config in index.php..
28 * 'Pman_I18N' => array(
30 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
39 'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
45 require_once 'Pman.php';
46 class Pman_I18N extends Pman
49 // these are the languages we support.
52 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
61 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
64 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
74 parent::getAuth(); // load company!
76 $au = $this->getAuthUser();
78 // $this->jerr("Not authenticated", array('authFailure' => true));
80 $this->authUser = $au;
82 $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
83 $opts = empty($opts) ? array() : $opts;
86 foreach($opts as $k=>$v) {
89 $this->cfg[$k] = $this->getDefaultCfg($k);
92 $this->cfg[$k] = is_array($v) ? $v : explode(',', $v);
100 // returns a list of all countries..
101 function getDefaultCfg($t) {
105 require_once 'I18Nv2/Country.php';
107 $c = new I18Nv2_Country('en');
108 $ret = array_keys($c->codes);
112 require_once 'I18Nv2/Language.php';
113 $c = new I18Nv2_Language('en');
114 $ret = array_keys($c->codes);
118 require_once 'I18Nv2/Currency.php';
119 $c = new I18Nv2_Currency('en');
120 $ret = array_keys($c->codes);
124 foreach ($ret as $k=>$v) {
125 $ret[$k] = strtoupper($v);
134 function setSession($au)
136 $this->authUser = $au;
137 $lbits = implode('_', $this->findLang());
138 if (empty($_SESSION['Pman_I18N'])) {
139 $_SESSION['Pman_I18N'] = array();
142 $_SESSION['Pman_I18N'][$lbits] = array(
143 'l' => $this->getList('l', $lbits),
144 'c' => $this->getList('c', $lbits),
145 'm' => $this->getList('m', $lbits),
151 function getList($type, $inlang,$fi=false)
153 //$l = new I18Nv2_Language($inlang);
154 //$c= new I18Nv2_Country($inlang);
155 $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
163 foreach($this->cfg[$type] as $k) {
164 if (is_array($filter) && !in_array($k, $filter)) {
170 'title' => $this->translate($inlang, $type, $k)
181 function findLang() {
183 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
184 $lbits = explode('_', strtoupper($lang));
185 $lbits[0] = strtolower($lbits[0]);
186 require_once 'I18Nv2/Country.php';
187 require_once 'I18Nv2/Language.php';
188 $langs = new I18Nv2_Language('en');
189 $countries = new I18Nv2_Country('en');
192 if (!isset($langs->codes[strtolower($lbits[0])])) {
193 $this->jerr('invalid lang');
195 if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
196 $this->jerr('invalid lang Country component');
208 $lbits = $this->findLang();
215 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
219 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
223 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
228 //DB_DataObject::debugLevel(1);
236 $this->jerr("ERROR");
243 function loadFilter($type)
245 // this code only applies to Clipping module
246 if (!$this->authUser) {
250 // this needs moving to it's own project
252 if (!$this->hasModule('Clipping')) {
259 //DB_DataObject::debugLevel(1);
260 $q = DB_DataObject::factory('Projects');
262 $c = DB_Dataobject::factory('Companies');
263 $c->get($this->authUser->company_id);
264 if ($c->comptype !='OWNER') {
265 $q->client_id = $this->authUser->company_id;
268 $col = ($type == 'l' ? 'languages' : 'countries');
269 $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
270 $q->whereAdd("LENGTH($col) > 0");
274 while ($q->fetch()) {
275 $bits = explode(',', $q->dval);
276 foreach($bits as $k) {
280 return array_keys($ret);
285 function translateList($au, $type, $k)
287 $ar = explode(',', $k);
289 foreach($ar as $kk) {
290 $ret[] = $this->translate($au, $type, $kk);
292 return implode(', ', $ret);
297 * require_once 'Pman/I18N.php';
298 * $x = new Pman_I18N();
299 * $x->translate($this->authuser, 'c', 'US');
300 * @param au - auth User
301 * @param type = 'c' or 'l'
302 * @param k - key to translate
306 function translate($au, $type, $k)
313 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
314 $lbits = explode('_', strtoupper($lang));
317 if (!isset($cache[$lang])) {
318 require_once 'I18Nv2/Country.php';
319 require_once 'I18Nv2/Language.php';
320 require_once 'I18Nv2/Currency.php';
321 $cache[$lang] = array(
322 'l' => new I18Nv2_Language($lang, 'UTF-8'),
323 'c' => new I18Nv2_Country($lang, 'UTF-8'),
324 'm' => new I18Nv2_Currency($lang, 'UTF-8')
326 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
329 return 'Other / Unknown';
334 $tolang = explode('_', $k);
336 $ret = $cache[$lang][$type]->getName($tolang[0]);
337 if (count($tolang) > 1) {
338 $ret.= '('.$tolang[1].')';
342 $ret = $cache[$lang][$type]->getName($k);
343 //print_r(array($k, $ret));
351 function buildDB($ltype= false, $inlang= false )
353 if ($ltype === false) {
355 die("OOPS NO LTYPE");
357 if ($inlang == '**') {
358 return; // dont bother building generic..
360 if ($inlang === false) {
361 foreach( $this->cfg['l'] as $l) {
362 $this->buildDB($ltype, $l);
367 $list = $this->cfg[$ltype];
369 //DB_DataObject::debugLevel(1);
371 foreach($list as $lkey) {
372 $x = DB_DataObject::factory('i18n');
376 if ($x->find(true)) {
378 $x->lval = $this->translate($inlang, $ltype, $lkey);
382 $x->lval = $this->translate($inlang, $ltype, $lkey);