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..
30 * 'Pman_Core_I18N' => array(
32 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
40 'c' => '*', // eg. all languages..
41 'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
46 require_once 'Pman.php';
48 class Pman_Core_i18N extends Pman
52 // these are the default languages we support.
55 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
64 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
67 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
77 parent::getAuth(); // load company!
79 $au = $this->getAuthUser();
81 // $this->jerr("Not authenticated", array('authFailure' => true));
83 $this->authUser = $au;
85 $opts = PEAR::getStaticProperty('Pman_Core_I18N', 'options');
87 $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
89 $opts = empty($opts) ? array() : $opts;
91 // load the cofiguration
92 foreach($opts as $k=>$v) {
95 $this->cfg[$k] = $this->getDefaultCfg($k);
98 $this->cfg[$k] = is_array($v) ? $v : explode(',', $v);
106 // returns a list of all countries..
107 function getDefaultCfg($t) {
111 require_once 'I18Nv2/Country.php';
113 $c = new I18Nv2_Country('en');
114 $ret = array_keys($c->codes);
118 require_once 'I18Nv2/Language.php';
119 $c = new I18Nv2_Language('en');
120 $ret = array_keys($c->codes);
124 require_once 'I18Nv2/Currency.php';
125 $c = new I18Nv2_Currency('en');
126 $ret = array_keys($c->codes);
130 foreach ($ret as $k=>$v) {
131 $ret[$k] = strtoupper($v);
140 function setSession($au)
142 $this->authUser = $au;
143 $lbits = implode('_', $this->findLang());
144 if (empty($_SESSION['Pman_I18N'])) {
145 $_SESSION['Pman_I18N'] = array();
148 $_SESSION['Pman_I18N'][$lbits] = array(
149 'l' => $this->getList('l', $lbits),
150 'c' => $this->getList('c', $lbits),
151 'm' => $this->getList('m', $lbits),
157 function getList($type, $inlang,$fi=false)
159 //$l = new I18Nv2_Language($inlang);
160 //$c= new I18Nv2_Country($inlang);
161 $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
169 foreach($this->cfg[$type] as $k) {
170 if (is_array($filter) && !in_array($k, $filter)) {
176 'title' => $this->translate($inlang, $type, $k)
187 function findLang() {
189 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
190 $lbits = explode('_', strtoupper($lang));
191 $lbits[0] = strtolower($lbits[0]);
192 require_once 'I18Nv2/Country.php';
193 require_once 'I18Nv2/Language.php';
194 $langs = new I18Nv2_Language('en');
195 $countries = new I18Nv2_Country('en');
198 if (!isset($langs->codes[strtolower($lbits[0])])) {
199 $this->jerr('invalid lang');
201 if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
202 $this->jerr('invalid lang Country component');
214 $lbits = $this->findLang();
221 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
225 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
229 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
234 //DB_DataObject::debugLevel(1);
242 $this->jerr("ERROR");
249 function loadFilter($type)
251 // this code only applies to Clipping module
252 if (!$this->authUser) {
256 // this needs moving to it's own project
258 if (!$this->hasModule('Clipping')) {
265 //DB_DataObject::debugLevel(1);
266 $q = DB_DataObject::factory('Projects');
268 $c = DB_Dataobject::factory('Companies');
269 $c->get($this->authUser->company_id);
270 if ($c->comptype !='OWNER') {
271 $q->client_id = $this->authUser->company_id;
274 $col = ($type == 'l' ? 'languages' : 'countries');
275 $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
276 $q->whereAdd("LENGTH($col) > 0");
280 while ($q->fetch()) {
281 $bits = explode(',', $q->dval);
282 foreach($bits as $k) {
286 return array_keys($ret);
291 function translateList($au, $type, $k)
293 $ar = explode(',', $k);
295 foreach($ar as $kk) {
296 $ret[] = $this->translate($au, $type, $kk);
298 return implode(', ', $ret);
303 * require_once 'Pman/I18N.php';
304 * $x = new Pman_I18N();
305 * $x->translate($this->authuser, 'c', 'US');
306 * @param au - auth User
307 * @param type = 'c' or 'l'
308 * @param k - key to translate
312 function translate($au, $type, $k)
319 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
320 $lbits = explode('_', strtoupper($lang));
323 if (!isset($cache[$lang])) {
324 require_once 'I18Nv2/Country.php';
325 require_once 'I18Nv2/Language.php';
326 require_once 'I18Nv2/Currency.php';
327 $cache[$lang] = array(
328 'l' => new I18Nv2_Language($lang, 'UTF-8'),
329 'c' => new I18Nv2_Country($lang, 'UTF-8'),
330 'm' => new I18Nv2_Currency($lang, 'UTF-8')
332 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
335 return 'Other / Unknown';
340 $tolang = explode('_', $k);
342 $ret = $cache[$lang][$type]->getName($tolang[0]);
343 if (count($tolang) > 1) {
344 $ret.= '('.$tolang[1].')';
348 $ret = $cache[$lang][$type]->getName($k);
349 //print_r(array($k, $ret));
357 function buildDB($ltype= false, $inlang= false )
359 if ($ltype === false) {
361 die("OOPS NO LTYPE");
363 if ($inlang == '**') {
364 return; // dont bother building generic..
366 if ($inlang === false) {
367 foreach( $this->cfg['l'] as $l) {
368 $this->buildDB($ltype, $l);
373 $list = $this->getDefaultCfg($ltype);
375 DB_DataObject::debugLevel(1);
377 foreach($list as $lkey) {
378 $x = DB_DataObject::factory('i18n');
382 if ($x->find(true)) {
384 $x->lval = $this->translate($inlang, $ltype, $lkey);
388 $x->lval = $this->translate($inlang, $ltype, $lkey);