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',
39 'c' => '*', // eg. all languages..
40 'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
45 require_once 'Pman.php';
47 class Pman_Core_i18N extends Pman
51 // these are the default languages we support.
54 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
63 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
66 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
76 parent::getAuth(); // load company!
78 $au = $this->getAuthUser();
80 // $this->jerr("Not authenticated", array('authFailure' => true));
82 $this->authUser = $au;
84 $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
85 $opts = empty($opts) ? array() : $opts;
88 foreach($opts as $k=>$v) {
91 $this->cfg[$k] = $this->getDefaultCfg($k);
94 $this->cfg[$k] = is_array($v) ? $v : explode(',', $v);
102 // returns a list of all countries..
103 function getDefaultCfg($t) {
107 require_once 'I18Nv2/Country.php';
109 $c = new I18Nv2_Country('en');
110 $ret = array_keys($c->codes);
114 require_once 'I18Nv2/Language.php';
115 $c = new I18Nv2_Language('en');
116 $ret = array_keys($c->codes);
120 require_once 'I18Nv2/Currency.php';
121 $c = new I18Nv2_Currency('en');
122 $ret = array_keys($c->codes);
126 foreach ($ret as $k=>$v) {
127 $ret[$k] = strtoupper($v);
136 function setSession($au)
138 $this->authUser = $au;
139 $lbits = implode('_', $this->findLang());
140 if (empty($_SESSION['Pman_I18N'])) {
141 $_SESSION['Pman_I18N'] = array();
144 $_SESSION['Pman_I18N'][$lbits] = array(
145 'l' => $this->getList('l', $lbits),
146 'c' => $this->getList('c', $lbits),
147 'm' => $this->getList('m', $lbits),
153 function getList($type, $inlang,$fi=false)
155 //$l = new I18Nv2_Language($inlang);
156 //$c= new I18Nv2_Country($inlang);
157 $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
165 foreach($this->cfg[$type] as $k) {
166 if (is_array($filter) && !in_array($k, $filter)) {
172 'title' => $this->translate($inlang, $type, $k)
183 function findLang() {
185 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
186 $lbits = explode('_', strtoupper($lang));
187 $lbits[0] = strtolower($lbits[0]);
188 require_once 'I18Nv2/Country.php';
189 require_once 'I18Nv2/Language.php';
190 $langs = new I18Nv2_Language('en');
191 $countries = new I18Nv2_Country('en');
194 if (!isset($langs->codes[strtolower($lbits[0])])) {
195 $this->jerr('invalid lang');
197 if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
198 $this->jerr('invalid lang Country component');
210 $lbits = $this->findLang();
217 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
221 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
225 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
230 //DB_DataObject::debugLevel(1);
238 $this->jerr("ERROR");
245 function loadFilter($type)
247 // this code only applies to Clipping module
248 if (!$this->authUser) {
252 // this needs moving to it's own project
254 if (!$this->hasModule('Clipping')) {
261 //DB_DataObject::debugLevel(1);
262 $q = DB_DataObject::factory('Projects');
264 $c = DB_Dataobject::factory('Companies');
265 $c->get($this->authUser->company_id);
266 if ($c->comptype !='OWNER') {
267 $q->client_id = $this->authUser->company_id;
270 $col = ($type == 'l' ? 'languages' : 'countries');
271 $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
272 $q->whereAdd("LENGTH($col) > 0");
276 while ($q->fetch()) {
277 $bits = explode(',', $q->dval);
278 foreach($bits as $k) {
282 return array_keys($ret);
287 function translateList($au, $type, $k)
289 $ar = explode(',', $k);
291 foreach($ar as $kk) {
292 $ret[] = $this->translate($au, $type, $kk);
294 return implode(', ', $ret);
299 * require_once 'Pman/I18N.php';
300 * $x = new Pman_I18N();
301 * $x->translate($this->authuser, 'c', 'US');
302 * @param au - auth User
303 * @param type = 'c' or 'l'
304 * @param k - key to translate
308 function translate($au, $type, $k)
315 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
316 $lbits = explode('_', strtoupper($lang));
319 if (!isset($cache[$lang])) {
320 require_once 'I18Nv2/Country.php';
321 require_once 'I18Nv2/Language.php';
322 require_once 'I18Nv2/Currency.php';
323 $cache[$lang] = array(
324 'l' => new I18Nv2_Language($lang, 'UTF-8'),
325 'c' => new I18Nv2_Country($lang, 'UTF-8'),
326 'm' => new I18Nv2_Currency($lang, 'UTF-8')
328 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
331 return 'Other / Unknown';
336 $tolang = explode('_', $k);
338 $ret = $cache[$lang][$type]->getName($tolang[0]);
339 if (count($tolang) > 1) {
340 $ret.= '('.$tolang[1].')';
344 $ret = $cache[$lang][$type]->getName($k);
345 //print_r(array($k, $ret));
353 function buildDB($ltype= false, $inlang= false )
355 if ($ltype === false) {
357 die("OOPS NO LTYPE");
359 if ($inlang == '**') {
360 return; // dont bother building generic..
362 if ($inlang === false) {
363 foreach( $this->cfg['l'] as $l) {
364 $this->buildDB($ltype, $l);
369 $list = $this->getDefaultCfg($ltype);
371 DB_DataObject::debugLevel(1);
373 foreach($list as $lkey) {
374 $x = DB_DataObject::factory('i18n');
378 if ($x->find(true)) {
380 $x->lval = $this->translate($inlang, $ltype, $lkey);
384 $x->lval = $this->translate($inlang, $ltype, $lkey);