3 /// provide language data!!!
4 // THIS IS PUBLICALLY ACCESSABLE..
7 * Either we load this as a standard array at start??
10 * or we treat it like a regular pulldown... - on demand...
12 * big snag is that we can not include these until the user has specified their interface language.
14 * If we do a global jS array?
15 * Pman.Lang.en = [ 'en' , 'English']
17 * If we do a standard 'store' version..
18 * { total: x, data [ { code: 'en', title: 'English' }] }
22 require_once 'Pman.php';
23 class Pman_I18N extends Pman
26 // these are the languages we support.
29 'en', 'zh_CN', 'zh_HK', 'zh_TW', 'th', 'ko', 'ja', 'ms',
38 'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
41 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
51 parent::getAuth(); // load company!
53 $au = $this->getAuthUser();
55 // $this->jerr("Not authenticated", array('authFailure' => true));
57 $this->authUser = $au;
59 $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
60 $opts = empty($opts) ? array() : $opts;
63 foreach($opts as $k=>$v) {
66 $this->cfg[$k] = $this->getDefaultCfg($k);
69 $this->cfg[$k] = explode(',', $v);
77 // returns a list of all countries..
78 function getDefaultCfg($t) {
82 require_once 'I18Nv2/Country.php';
84 $c = new I18Nv2_Country('en');
85 $ret = array_keys($c->codes);
88 require_once 'I18Nv2/Language.php';
89 $c = new I18Nv2_Language('en');
90 $ret = array_keys($c->codes);
93 require_once 'I18Nv2/Currency.php';
94 $c = new I18Nv2_Currency('en');
95 $ret = array_keys($c->codes);
98 foreach ($ret as $k=>$v) {
99 $ret[$k] = strtoupper($v);
108 function setSession($au)
110 $this->authUser = $au;
111 $lbits = implode('_', $this->findLang());
112 if (empty($_SESSION['Pman_I18N'])) {
113 $_SESSION['Pman_I18N'] = array();
116 $_SESSION['Pman_I18N'][$lbits] = array(
117 'l' => $this->getList('l', $lbits),
118 'c' => $this->getList('c', $lbits),
119 'm' => $this->getList('m', $lbits),
125 function getList($type, $inlang,$fi=false)
127 //$l = new I18Nv2_Language($inlang);
128 //$c= new I18Nv2_Country($inlang);
129 $filter = !$fi ? false : $this->loadFilter($type); // project specific languages..
137 foreach($this->cfg[$type] as $k) {
138 if (is_array($filter) && !in_array($k, $filter)) {
144 'title' => $this->translate($inlang, $type, $k)
155 function findLang() {
157 $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
158 $lbits = explode('_', strtoupper($lang));
159 $lbits[0] = strtolower($lbits[0]);
160 require_once 'I18Nv2/Country.php';
161 require_once 'I18Nv2/Language.php';
162 $langs = new I18Nv2_Language('en');
163 $countries = new I18Nv2_Country('en');
166 if (!isset($langs->codes[strtolower($lbits[0])])) {
167 $this->jerr('invalid lang');
169 if (!empty($lbits[1]) && !isset($countries->codes[$lbits[1]])) {
170 $this->jerr('invalid lang Country component');
182 $lbits = $this->findLang();
189 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
193 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
197 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
202 //DB_DataObject::debugLevel(1);
210 $this->jerr("ERROR");
217 function loadFilter($type)
219 // this code only applies to Clipping module
220 if (!$this->authUser) {
224 // this needs moving to it's own project
226 if (!$this->hasModule('Clipping')) {
233 //DB_DataObject::debugLevel(1);
234 $q = DB_DataObject::factory('Projects');
236 $c = DB_Dataobject::factory('Companies');
237 $c->get($this->authUser->company_id);
238 if ($c->comptype !='OWNER') {
239 $q->client_id = $this->authUser->company_id;
242 $col = ($type == 'l' ? 'languages' : 'countries');
243 $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
244 $q->whereAdd("LENGTH($col) > 0");
248 while ($q->fetch()) {
249 $bits = explode(',', $q->dval);
250 foreach($bits as $k) {
254 return array_keys($ret);
259 function translateList($au, $type, $k)
261 $ar = explode(',', $k);
263 foreach($ar as $kk) {
264 $ret[] = $this->translate($au, $type, $kk);
266 return implode(', ', $ret);
271 * require_once 'Pman/I18N.php';
272 * $x = new Pman_I18N();
273 * $x->translate($this->authuser, 'c', 'US');
274 * @param au - auth User
275 * @param type = 'c' or 'l'
276 * @param k - key to translate
280 function translate($au, $type, $k)
287 $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
288 $lbits = explode('_', strtoupper($lang));
291 if (!isset($cache[$lang])) {
292 require_once 'I18Nv2/Country.php';
293 require_once 'I18Nv2/Language.php';
294 require_once 'I18Nv2/Currency.php';
295 $cache[$lang] = array(
296 'l' => new I18Nv2_Language($lang, 'UTF-8'),
297 'c' => new I18Nv2_Country($lang, 'UTF-8'),
298 'm' => new I18Nv2_Currency($lang, 'UTF-8')
300 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
303 return 'Other / Unknown';
308 $tolang = explode('_', $k);
310 $ret = $cache[$lang][$type]->getName($tolang[0]);
311 if (count($tolang) > 1) {
312 $ret.= '('.$tolang[1].')';
316 $ret = $cache[$lang][$type]->getName($k);
317 //print_r(array($k, $ret));
325 function buildDB($ltype= false, $inlang= false )
327 if ($ltype === false) {
329 die("OOPS NO LTYPE");
332 if ($inlang === false) {
333 foreach( $this->cfg['l'] as $l) {
334 $this->buildDB($ltype, $l);
339 $list = $this->cfg[$ltype];
341 foreach($list as $lkey) {
342 $x = DB_DataObject::factory('i18n');
346 if ($x->find(true)) {
348 $x->lval = $this->translate($inlang, $ltype, $lkey);
352 $x->lval = $this->translate($inlang, $ltype, $lkey);