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..
27 * 'Pman_Core_I18N' => array(
29 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
38 'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
43 require_once 'Pman.php';
45 class Pman_Core_i18N extends Pman
49 // anyone can downlaod this list - as it's needed prior to the login box.
54 // these are the languages we support.
57 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
66 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
69 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
79 parent::getAuth(); // load company!
81 $au = $this->getAuthUser();
83 // $this->jerr("Not authenticated", array('authFailure' => true));
85 $this->authUser = $au;
87 $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
88 $opts = empty($opts) ? array() : $opts;
91 foreach($opts as $k=>$v) {
94 $this->cfg[$k] = $this->getDefaultCfg($k);
97 $this->cfg[$k] = is_array($v) ? $v : explode(',', $v);
105 // returns a list of all countries..
106 function getDefaultCfg($t) {
110 require_once 'I18Nv2/Country.php';
112 $c = new I18Nv2_Country('en');
113 $ret = array_keys($c->codes);
117 require_once 'I18Nv2/Language.php';
118 $c = new I18Nv2_Language('en');
119 $ret = array_keys($c->codes);
123 require_once 'I18Nv2/Currency.php';
124 $c = new I18Nv2_Currency('en');
125 $ret = array_keys($c->codes);
129 foreach ($ret as $k=>$v) {
130 $ret[$k] = strtoupper($v);
139 function setSession($au)
141 $this->authUser = $au;
142 $lbits = implode('_', $this->findLang());
143 if (empty($_SESSION['Pman_I18N'])) {
144 $_SESSION['Pman_I18N'] = array();
147 $_SESSION['Pman_I18N'][$lbits] = array(
148 'l' => $this->getList('l', $lbits),
149 'c' => $this->getList('c', $lbits),
150 'm' => $this->getList('m', $lbits),
156 function getList($type, $inlang,$fi=false)
158 //$l = new I18Nv2_Language($inlang);
159 //$c= new I18Nv2_Country($inlang);
160 $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
168 foreach($this->cfg[$type] as $k) {
169 if (is_array($filter) && !in_array($k, $filter)) {
175 'title' => $this->translate($inlang, $type, $k)
186 function findLang() {
188 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
189 $lbits = explode('_', strtoupper($lang));
190 $lbits[0] = strtolower($lbits[0]);
191 require_once 'I18Nv2/Country.php';
192 require_once 'I18Nv2/Language.php';
193 $langs = new I18Nv2_Language('en');
194 $countries = new I18Nv2_Country('en');
197 if (!isset($langs->codes[strtolower($lbits[0])])) {
198 $this->jerr('invalid lang');
200 if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
201 $this->jerr('invalid lang Country component');
213 $lbits = $this->findLang();
220 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
224 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
228 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
233 //DB_DataObject::debugLevel(1);
241 $this->jerr("ERROR");
248 function loadFilter($type)
250 // this code only applies to Clipping module
251 if (!$this->authUser) {
255 // this needs moving to it's own project
257 if (!$this->hasModule('Clipping')) {
264 //DB_DataObject::debugLevel(1);
265 $q = DB_DataObject::factory('Projects');
267 $c = DB_Dataobject::factory('Companies');
268 $c->get($this->authUser->company_id);
269 if ($c->comptype !='OWNER') {
270 $q->client_id = $this->authUser->company_id;
273 $col = ($type == 'l' ? 'languages' : 'countries');
274 $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
275 $q->whereAdd("LENGTH($col) > 0");
279 while ($q->fetch()) {
280 $bits = explode(',', $q->dval);
281 foreach($bits as $k) {
285 return array_keys($ret);
290 function translateList($au, $type, $k)
292 $ar = explode(',', $k);
294 foreach($ar as $kk) {
295 $ret[] = $this->translate($au, $type, $kk);
297 return implode(', ', $ret);
302 * require_once 'Pman/I18N.php';
303 * $x = new Pman_I18N();
304 * $x->translate($this->authuser, 'c', 'US');
305 * @param au - auth User
306 * @param type = 'c' or 'l'
307 * @param k - key to translate
311 function translate($au, $type, $k)
318 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
319 $lbits = explode('_', strtoupper($lang));
322 if (!isset($cache[$lang])) {
323 require_once 'I18Nv2/Country.php';
324 require_once 'I18Nv2/Language.php';
325 require_once 'I18Nv2/Currency.php';
326 $cache[$lang] = array(
327 'l' => new I18Nv2_Language($lang, 'UTF-8'),
328 'c' => new I18Nv2_Country($lang, 'UTF-8'),
329 'm' => new I18Nv2_Currency($lang, 'UTF-8')
331 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
334 return 'Other / Unknown';
339 $tolang = explode('_', $k);
341 $ret = $cache[$lang][$type]->getName($tolang[0]);
342 if (count($tolang) > 1) {
343 $ret.= '('.$tolang[1].')';
347 $ret = $cache[$lang][$type]->getName($k);
348 //print_r(array($k, $ret));
356 function buildDB($ltype= false, $inlang= false )
358 if ($ltype === false) {
360 die("OOPS NO LTYPE");
362 if ($inlang == '**') {
363 return; // dont bother building generic..
365 if ($inlang === false) {
366 foreach( $this->cfg['l'] as $l) {
367 $this->buildDB($ltype, $l);
372 $list = $this->getDefaultCfg($ltype);
374 DB_DataObject::debugLevel(1);
376 foreach($list as $lkey) {
377 $x = DB_DataObject::factory('i18n');
381 if ($x->find(true)) {
383 $x->lval = $this->translate($inlang, $ltype, $lkey);
387 $x->lval = $this->translate($inlang, $ltype, $lkey);