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 are supported by the old code....
23 * Database language translation should be done using the database table.
24 * So sorting can be done correctly..
26 * Configuration in index.php..
28 * 'Pman_Core_I18N' => array(
30 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
38 'c' => '*', // eg. all languages..
39 'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
44 require_once 'Pman.php';
46 class Pman_Core_i18N extends Pman
50 // these are the default languages we support.
53 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
62 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
65 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
75 parent::getAuth(); // load company!
77 $au = $this->getAuthUser();
79 // $this->jerr("Not authenticated", array('authFailure' => true));
81 $this->authUser = $au;
83 $opts = PEAR::getStaticProperty('Pman_Core_I18N', 'options');
85 $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
87 $opts = empty($opts) ? array() : $opts;
89 // load the cofiguration
90 foreach($opts as $k=>$v) {
93 $this->cfg[$k] = $this->getDefaultCfg($k);
96 $this->cfg[$k] = is_array($v) ? $v : explode(',', $v);
104 // returns a list of all countries..
105 function getDefaultCfg($t) {
109 require_once 'I18Nv2/Country.php';
111 $c = new I18Nv2_Country('en');
112 $ret = array_keys($c->codes);
116 require_once 'I18Nv2/Language.php';
117 $c = new I18Nv2_Language('en');
118 $ret = array_keys($c->codes);
122 require_once 'I18Nv2/Currency.php';
123 $c = new I18Nv2_Currency('en');
124 $ret = array_keys($c->codes);
128 foreach ($ret as $k=>$v) {
129 $ret[$k] = strtoupper($v);
138 function setSession($au)
140 $this->authUser = $au;
141 $lbits = implode('_', $this->findLang());
142 if (empty($_SESSION['Pman_I18N'])) {
143 $_SESSION['Pman_I18N'] = array();
146 $_SESSION['Pman_I18N'][$lbits] = array(
147 'l' => $this->getList('l', $lbits),
148 'c' => $this->getList('c', $lbits),
149 'm' => $this->getList('m', $lbits),
155 function getList($type, $inlang,$fi=false)
157 //$l = new I18Nv2_Language($inlang);
158 //$c= new I18Nv2_Country($inlang);
159 $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
167 foreach($this->cfg[$type] as $k) {
168 if (is_array($filter) && !in_array($k, $filter)) {
174 'title' => $this->translate($inlang, $type, $k)
185 function findLang() {
187 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
188 $lbits = explode('_', strtoupper($lang));
189 $lbits[0] = strtolower($lbits[0]);
190 require_once 'I18Nv2/Country.php';
191 require_once 'I18Nv2/Language.php';
192 $langs = new I18Nv2_Language('en');
193 $countries = new I18Nv2_Country('en');
196 if (!isset($langs->codes[strtolower($lbits[0])])) {
197 $this->jerr('invalid lang');
199 if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
200 $this->jerr('invalid lang Country component');
212 $lbits = $this->findLang();
219 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
223 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
227 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
232 //DB_DataObject::debugLevel(1);
240 $this->jerr("ERROR");
247 function loadFilter($type)
249 // this code only applies to Clipping module
250 if (!$this->authUser) {
254 // this needs moving to it's own project
256 if (!$this->hasModule('Clipping')) {
263 //DB_DataObject::debugLevel(1);
264 $q = DB_DataObject::factory('Projects');
266 $c = DB_Dataobject::factory('Companies');
267 $c->get($this->authUser->company_id);
268 if ($c->comptype !='OWNER') {
269 $q->client_id = $this->authUser->company_id;
272 $col = ($type == 'l' ? 'languages' : 'countries');
273 $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
274 $q->whereAdd("LENGTH($col) > 0");
278 while ($q->fetch()) {
279 $bits = explode(',', $q->dval);
280 foreach($bits as $k) {
284 return array_keys($ret);
289 function translateList($au, $type, $k)
291 $ar = explode(',', $k);
293 foreach($ar as $kk) {
294 $ret[] = $this->translate($au, $type, $kk);
296 return implode(', ', $ret);
301 * require_once 'Pman/I18N.php';
302 * $x = new Pman_I18N();
303 * $x->translate($this->authuser, 'c', 'US');
304 * @param au - auth User
305 * @param type = 'c' or 'l'
306 * @param k - key to translate
310 function translate($au, $type, $k)
317 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
318 $lbits = explode('_', strtoupper($lang));
321 if (!isset($cache[$lang])) {
322 require_once 'I18Nv2/Country.php';
323 require_once 'I18Nv2/Language.php';
324 require_once 'I18Nv2/Currency.php';
325 $cache[$lang] = array(
326 'l' => new I18Nv2_Language($lang, 'UTF-8'),
327 'c' => new I18Nv2_Country($lang, 'UTF-8'),
328 'm' => new I18Nv2_Currency($lang, 'UTF-8')
330 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
333 return 'Other / Unknown';
338 $tolang = explode('_', $k);
340 $ret = $cache[$lang][$type]->getName($tolang[0]);
341 if (count($tolang) > 1) {
342 $ret.= '('.$tolang[1].')';
346 $ret = $cache[$lang][$type]->getName($k);
347 //print_r(array($k, $ret));
355 function buildDB($ltype= false, $inlang= false )
357 if ($ltype === false) {
359 die("OOPS NO LTYPE");
361 if ($inlang == '**') {
362 return; // dont bother building generic..
364 if ($inlang === false) {
365 foreach( $this->cfg['l'] as $l) {
366 $this->buildDB($ltype, $l);
371 $list = $this->getDefaultCfg($ltype);
373 DB_DataObject::debugLevel(1);
375 foreach($list as $lkey) {
376 $x = DB_DataObject::factory('i18n');
380 if ($x->find(true)) {
382 $x->lval = $this->translate($inlang, $ltype, $lkey);
386 $x->lval = $this->translate($inlang, $ltype, $lkey);