3 * Table Definition for i18n
5 * This is heavily related to the Pman_I18n implementation..
7 * It should eventually replace most of that..
10 require_once 'DB/DataObject.php';
12 class Pman_Core_DataObjects_I18n extends DB_DataObject
15 /* the code below is auto generated do not remove the above tag */
17 public $__table = 'i18n'; // table name
18 public $id; // int(11) not_null primary_key auto_increment
19 public $ltype; // string(1) not_null multiple_key
20 public $lkey; // string(8) not_null
21 public $inlang; // string(8) not_null
22 public $lval; // string(64) not_null
25 /* the code above is auto generated do not remove the tag below */
28 // we have a small default set of languages available..
29 // you can modify this by making this setting in the index.php loader.
30 // Pman_Core_i18n = array( 'c' => *, 'l' => '*', 'm' => '*')
33 // translated versions availalable
36 'en', 'zh_CN', 'zh_HK',
38 // languages available
41 'en', 'zh_CN', 'zh_HK', 'zh_TW', //'th', 'ko', 'ja', 'ms',
50 //'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
53 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
55 'add_l'=> array(), // key -> value additional languages...
56 'add_c'=> array(), // additional countries...(eg. '-R' => 'Regional' )
57 'add_m'=> array(), // additional currencies...
62 * initalizie the cfg aray
67 /*static $loaded = false;
73 $ff= HTML_FlexyFramework::get();
75 // BC compatible.. if any of these are set, then we use them as the settings..
77 foreach(array('Pman_Core_I18N', 'Pman_I18N','Pman_I18n') as $pk) {
78 if (isset($ff->$pk)) {
84 echo '<PRE>';print_R($opts);//exit;
87 // var_dump($opts);exit;
89 $i = DB_DataObject::Factory('I18n');
90 // load the cofiguration
91 foreach($opts as $k=>$v) {
93 if ($v == '*') { // everything..
94 self::$cfg[$k] = $i->availableCodes($k, false);
97 echo "$k / "; //print_R($v);
98 self::$cfg[$k] = is_array($v) ? $v : explode(',', $v);
100 print_R(self::$cfg);exit;
110 // the default configuration.
112 function applyFilters($q, $au)
115 //DB_DataObject::debugLevel(1);
116 if (!empty($q['query']['_with_en'])) {
118 $this->buildDB(); // ensure we have the full database...
121 i18n_translate(ltype, lkey, 'en') as lval_en
125 if (!empty($q['query']['name'])) {
126 //DB_DAtaObject::debugLevel(1);
127 $v = strtoupper($this->escape($q['query']['name']));
128 $this->whereAdd("upper(lval) LIKE '%{$v}%'");
131 if (!empty($q['_filtered']) && !empty($this->ltype)) {
133 $filter = $cfg[$this->ltype];
134 $this->whereAddIn('lkey', $filter, 'string');
140 function translate($inlang,$ltype,$kval)
143 $x = DB_DataObject::factory('i18n');
147 $fallback = clone($x);
150 if ($x->find(true) && !empty($x->lval)) {
153 $fallback->inlang = 'en';
154 if ($fallback->find(true) && !empty($fallback->lval)) {
155 return $fallback->lval;
162 function toTransList($ltype, $inlang)
166 $this->ltype = $ltype;
167 $this->inlang= $inlang;
169 $this->selectAdd('lkey as code, lval as title');
173 while ($this->fetch()) {
175 'code' => $this->code,
176 'title' => $this->title
185 // -------------- code to handle importing into database..
190 // returns a list of all countries/languages etc.. (with '*')
191 function availableCodes($t, $filtered = true)
198 require_once 'I18Nv2/Country.php';
200 $c = new I18Nv2_Country('en');
201 $ret = array_keys($c->codes);
202 if (!empty($cfg['add_c'])) {
203 $ret = array_merge($ret, array_keys($cfg['add_c']));
210 echo '<PRE>';print_R($cfg); print_r($ret); exit;
214 require_once 'I18Nv2/Language.php';
215 $c = new I18Nv2_Language('en');
216 $ret = array_keys($c->codes); // we need to make sure these are lowercase!!!
219 foreach ($cfg['add_l'] as $k=>$v){
220 // make sure that add_l is formated correctly.. (lower_UPPER?)
221 $tolang = explode('_', $k);
222 $tolang[0] = strtolower($tolang[0]);
223 $tolang = implode('_', $tolang);
225 unset($cfg['add_l'][$k]); // if they match..unset first.. then set
227 $cfg['add_l'][$tolang] = $v;
229 if (!empty($cfg['add_l'])) {
230 $ret = array_merge($ret, array_keys($cfg['add_l']));
236 require_once 'I18Nv2/Currency.php';
237 $c = new I18Nv2_Currency('en');
238 $ret = array_keys($c->codes);
239 if (!empty($cfg['add_m'])) {
240 $ret = array_merge($ret, array_keys($cfg['add_m']));
248 if ($filtered && !empty($cfg[$t]) && is_array($cfg[$t])) {
249 // then there is a filter. - we should include all of them, even if they are not relivatn??
250 return $cfg[$t]; //array_intersect($cfg[$t], $ret);
254 // why upper case everyting?!?!?
256 //foreach ($ret as $k=>$v) {
257 // $ret[$k] = ($t=='l') ? $ret[$k] : strtoupper($v);
264 function buildDB($ltype= false, $inlang= false )
269 if ($ltype === false) {
270 // trigger all builds.
271 //DB_DataObject::debugLevel(1);
278 if ($inlang == '**') {
279 return; // dont bother building generic..
283 if ($inlang === false) {
284 // do we want to add our 'configured ones..'
285 // We only build translatiosn for our configured ones..
286 //foreach( $this->availableCodes('l') as $l) {
288 foreach( $cfg['t'] as $l) {
289 $this->buildDB($ltype, $l);
295 //DB_DataObject::debugLevel(1);
296 $x = DB_DataObject::factory('I18n');
300 $complete = $x->fetchAll('lkey');
302 $list = $this->availableCodes($ltype);
303 echo '<PRE>'; print_r($list);
305 foreach($list as $lkey) {
306 // skip ones we know we have done...
307 if (in_array($lkey, $complete)) {
310 if (empty($lkey)) { // not sure why we get empty values here.
313 $x = DB_DataObject::factory('I18n');
317 if ($x->find(true)) {
319 $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
323 $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
332 * default translate - use i18n classes to provide a value.
337 function defaultTranslate($lang, $type, $k)
348 //$lbits = explode('_', strtoupper($lang));
349 $lbits = explode('_', $lang);
353 if (!isset($cache[$lang])) {
354 require_once 'I18Nv2/Country.php';
355 require_once 'I18Nv2/Language.php';
356 require_once 'I18Nv2/Currency.php';
357 $cache[$lang] = array(
358 'l' => new I18Nv2_Language($lang, 'UTF-8'),
359 'c' => new I18Nv2_Country($lang, 'UTF-8'),
360 'm' => new I18Nv2_Currency($lang, 'UTF-8')
362 //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
366 return 'Other / Unknown';
370 // for languages if we get zh_HK then we write out Chinese ( HK )
373 if ($type == 'l' && strpos($k, '_') > -1) {
374 $tolang = explode('_', $k);
375 $ret = $cache[$lang][$type]->getName(strtolower($tolang[0])) . '('.$tolang[1].')';
378 $ret = $cache[$lang][$type]->getName($k);
381 if ($orig_lang == 'zh_HK' || $orig_lang == 'zh_TW' ) {
382 // then translation is by default in simplified.
384 $ret = @iconv("UTF-8", "GB2312//IGNORE", $ret);
386 $ret = @iconv("GB2312", "BIG5//IGNORE", $ret);
389 $ret = @iconv("BIG5", "UTF-8//IGNORE", $ret);
395 // our wierd countries/langs etc..
396 if (isset($cfg['add_' . $type][$k])) {
397 return $cfg['add_' . $type][$k];