aad7ea7e90a5d2560e30633607101374ae5063c4
[Pman.Core] / DataObjects / I18n.php
1 <?php
2 /**
3  * Table Definition for i18n
4  *
5  * This is heavily related to the Pman_I18n implementation..
6  *
7  * It should eventually replace most of that..
8  * 
9  */
10 require_once 'DB/DataObject.php';
11
12 class Pman_Core_DataObjects_I18n extends DB_DataObject 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
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
23
24     
25     /* the code above is auto generated do not remove the tag below */
26     ###END_AUTOCODE
27     
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' => '*')
31     
32     static $cfg = array(
33         // translated versions availalable
34         
35         't' => array(
36             'en', 'zh_CN',   'zh_HK', 
37         ),
38         // languages available
39         'l' => array(
40             
41             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
42             'id', // indonesian
43             'tl', // tagalog
44             'vi', //vietnamise
45             'hi', // hindi
46             'ta', // tamil
47             '**', // other
48         ),
49         'c' => array(
50              'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
51         ),
52         'm' => array(
53             'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
54         )
55     );
56     
57     
58       // the default configuration.
59     
60     function applyFilters($q, $au)
61     {
62         
63         //DB_DataObject::debugLevel(1);
64         if (!empty($q['query']['_with_en'])) {
65             $this->selectAdd("
66                 i18n_translate(ltype, lkey, 'en') as lval_en
67                 
68             ");
69         }
70     }
71     
72     
73     
74     
75     
76     
77     
78     
79     
80     
81     // -------------- code to handle importing into database..
82     
83     
84     
85     
86     // returns a list of all countries/languages etc.. (with '*')
87     function availableCodes($t)
88     {
89         $ret = array();
90         switch ($t) {
91             case 'c':
92                 require_once 'I18Nv2/Country.php';
93                 
94                 $c = new I18Nv2_Country('en');
95                 $ret =  array_keys($c->codes);
96                 $ret[] = '**';
97                 break;
98             case 'l':
99                 require_once 'I18Nv2/Language.php';
100                 $c = new I18Nv2_Language('en');
101                 $ret =  array_keys($c->codes);
102                 $ret[] = '**';
103                 break;
104             case 'm':
105                 require_once 'I18Nv2/Currency.php';
106                 $c = new I18Nv2_Currency('en');
107                 $ret =  array_keys($c->codes);
108                 $ret[] = '**';
109                 break;
110         }
111         
112         foreach ($ret as $k=>$v) {
113             $ret[$k] = strtoupper($v);
114         }
115         
116         
117         return $ret;
118     }
119     
120     
121     function buildDB($ltype= false, $inlang= false )
122     {
123         if ($ltype === false) {
124             // trigger all builds.
125             DB_DataObject::debugLevel(1);
126             $this->buildDB('c');
127             $this->buildDB('l');
128             $this->buildDB('m');
129             return;
130         }
131         if ($inlang == '**') {
132             return; // dont bother building generic..
133         }
134         
135         
136         if ($inlang === false) {
137             // do we want to add our 'configured ones..'
138             // We only build translatiosn for our configured ones..
139             //foreach( $this->availableCodes('l') as $l) {
140                 
141             foreach( $this->cfg['t'] as $l) {
142                 $this->buildDB($ltype, $l);
143             }
144             return;
145         }
146         
147         $list =  $this->availableCodes($ltype);
148         
149         //DB_DataObject::debugLevel(1);
150         
151         foreach($list as $lkey) {
152             $x = DB_DataObject::factory('i18n');
153             $x->ltype = $ltype;
154             $x->lkey = $lkey;
155             $x->inlang= $inlang;
156             if ($x->find(true)) {
157                 $xx= clone($x);
158                 $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
159                 $x->update($xx);
160                 continue;
161             }
162             $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
163             $x->insert();
164             
165         }
166          
167         
168     }
169     
170     /**
171      * default translate  - use i18n classes to provide a value.
172      *
173      * 
174      */
175      
176     function defaultTranslate($lang, $type, $k) 
177     {
178       
179         static $cache;
180         
181         if (empty($k)) {
182             return '??';
183         }
184
185         $lbits = explode('_', strtoupper($lang));
186         $lang = $lbits[0];
187         
188         if (!isset($cache[$lang])) {
189             require_once 'I18Nv2/Country.php';
190             require_once 'I18Nv2/Language.php';
191             require_once 'I18Nv2/Currency.php';
192             $cache[$lang] = array(
193                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
194                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
195                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
196             );
197             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
198         }
199         
200         if ($k == '**') {
201             return 'Other / Unknown';
202         }
203     
204         
205         if ($type == 'l') {
206             $tolang = explode('_', $k);
207          
208             $ret = $cache[$lang][$type]->getName($tolang[0]);
209             if (count($tolang) > 1) {
210                 $ret.= '('.$tolang[1].')'; 
211             }
212             return $ret;
213         }
214         $ret = $cache[$lang][$type]->getName($k);
215         //print_r(array($k, $ret));
216         return $ret;
217         
218         
219     }
220     
221     
222 }