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
12 en : [ { code : 'end', title : 'English' }, .... ],
19 * index.php/Pman/I18N/BuildDB -- buildes the database..
20 * .. other formats are depreciated, but still supported..
25 * Configuration in index.php..
29 * 'Pman_Core_I18N' => array(
31 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
40 'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
45 require_once 'Pman.php';
47 class Pman_Core_i18N extends Pman
51 // anyone can downlaod this list - as it's needed prior to the login box.
56 // these are the languages we support.
59 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
68 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
71 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
81 parent::getAuth(); // load company!
83 $au = $this->getAuthUser();
85 // $this->jerr("Not authenticated", array('authFailure' => true));
87 $this->authUser = $au;
89 $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
90 $opts = empty($opts) ? array() : $opts;
93 foreach($opts as $k=>$v) {
96 $this->cfg[$k] = $this->getDefaultCfg($k);
99 $this->cfg[$k] = is_array($v) ? $v : explode(',', $v);
107 // returns a list of all countries..
108 function getDefaultCfg($t) {
112 require_once 'I18Nv2/Country.php';
114 $c = new I18Nv2_Country('en');
115 $ret = array_keys($c->codes);
119 require_once 'I18Nv2/Language.php';
120 $c = new I18Nv2_Language('en');
121 $ret = array_keys($c->codes);
125 require_once 'I18Nv2/Currency.php';
126 $c = new I18Nv2_Currency('en');
127 $ret = array_keys($c->codes);
131 foreach ($ret as $k=>$v) {
132 $ret[$k] = strtoupper($v);
141 function setSession($au)
143 $this->authUser = $au;
144 $lbits = implode('_', $this->findLang());
145 if (empty($_SESSION['Pman_I18N'])) {
146 $_SESSION['Pman_I18N'] = array();
149 $_SESSION['Pman_I18N'][$lbits] = array(
150 'l' => $this->getList('l', $lbits),
151 'c' => $this->getList('c', $lbits),
152 'm' => $this->getList('m', $lbits),
158 function getList($type, $inlang,$fi=false)
160 //$l = new I18Nv2_Language($inlang);
161 //$c= new I18Nv2_Country($inlang);
162 $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
170 foreach($this->cfg[$type] as $k) {
171 if (is_array($filter) && !in_array($k, $filter)) {
177 'title' => $this->translate($inlang, $type, $k)
188 function findLang() {
190 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
191 $lbits = explode('_', strtoupper($lang));
192 $lbits[0] = strtolower($lbits[0]);
193 require_once 'I18Nv2/Country.php';
194 require_once 'I18Nv2/Language.php';
195 $langs = new I18Nv2_Language('en');
196 $countries = new I18Nv2_Country('en');
199 if (!isset($langs->codes[strtolower($lbits[0])])) {
200 $this->jerr('invalid lang');
202 if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
203 $this->jerr('invalid lang Country component');
215 $lbits = $this->findLang();
222 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
226 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
230 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
235 //DB_DataObject::debugLevel(1);
243 $this->jerr("ERROR");
250 function loadFilter($type)
252 // this code only applies to Clipping module
253 if (!$this->authUser) {
257 // this needs moving to it's own project
259 if (!$this->hasModule('Clipping')) {
266 //DB_DataObject::debugLevel(1);
267 $q = DB_DataObject::factory('Projects');
269 $c = DB_Dataobject::factory('Companies');
270 $c->get($this->authUser->company_id);
271 if ($c->comptype !='OWNER') {
272 $q->client_id = $this->authUser->company_id;
275 $col = ($type == 'l' ? 'languages' : 'countries');
276 $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
277 $q->whereAdd("LENGTH($col) > 0");
281 while ($q->fetch()) {
282 $bits = explode(',', $q->dval);
283 foreach($bits as $k) {
287 return array_keys($ret);
292 function translateList($au, $type, $k)
294 $ar = explode(',', $k);
296 foreach($ar as $kk) {
297 $ret[] = $this->translate($au, $type, $kk);
299 return implode(', ', $ret);
304 * require_once 'Pman/I18N.php';
305 * $x = new Pman_I18N();
306 * $x->translate($this->authuser, 'c', 'US');
307 * @param au - auth User
308 * @param type = 'c' or 'l'
309 * @param k - key to translate
313 function translate($au, $type, $k)
320 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
321 $lbits = explode('_', strtoupper($lang));
324 if (!isset($cache[$lang])) {
325 require_once 'I18Nv2/Country.php';
326 require_once 'I18Nv2/Language.php';
327 require_once 'I18Nv2/Currency.php';
328 $cache[$lang] = array(
329 'l' => new I18Nv2_Language($lang, 'UTF-8'),
330 'c' => new I18Nv2_Country($lang, 'UTF-8'),
331 'm' => new I18Nv2_Currency($lang, 'UTF-8')
333 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
336 return 'Other / Unknown';
341 $tolang = explode('_', $k);
343 $ret = $cache[$lang][$type]->getName($tolang[0]);
344 if (count($tolang) > 1) {
345 $ret.= '('.$tolang[1].')';
349 $ret = $cache[$lang][$type]->getName($k);
350 //print_r(array($k, $ret));
358 function buildDB($ltype= false, $inlang= false )
360 if ($ltype === false) {
362 die("OOPS NO LTYPE");
364 if ($inlang == '**') {
365 return; // dont bother building generic..
367 if ($inlang === false) {
368 foreach( $this->cfg['l'] as $l) {
369 $this->buildDB($ltype, $l);
374 $list = $this->getDefaultCfg($ltype);
376 DB_DataObject::debugLevel(1);
378 foreach($list as $lkey) {
379 $x = DB_DataObject::factory('i18n');
383 if ($x->find(true)) {
385 $x->lval = $this->translate($inlang, $ltype, $lkey);
389 $x->lval = $this->translate($inlang, $ltype, $lkey);