I18n.php
[Pman.Core] / I18n.php
1 <?php
2 /**
3  * 
4  * Either we load this as a standard array at start??
5  * eg. after login...
6  * 
7  * We basically load up all supported languages at the start of the application.
8  * 
9  * By default it returns
10  * 
11  * Pman.I18n.Data = {
12       
13       en : {
14           l :  [ { code : 'en', title : 'English' }, .... ],
15           c :  [ { code : 'UK', title : 'United Kingdom' }, .... ],
16           m :  [ { code : 'USD', title : 'US Dollars' }, .... ],
17       fr : ....
18    }
19  * 
20  * 
21  * other usage:
22  * 
23  * index.php/Pman/I18N/BuildDB -- buildes the database..
24  * .. other formats are depreciated, but are supported by the old code....
25  * 
26  * 
27  * Database language translation should be done using the database table.
28  * So sorting can be done correctly..
29  * 
30  * Configuration in index.php..
31  * 
32  *  'Pman_Core_I18N' => array(
33       'l' => array(
34             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
35             'id', // indonesian
36             'tl', // tagalog
37             'vi', //vietnamise
38             'hi', // hindi
39             'ta', // tamil
40             '**', // other
41         ), 
42        'c' => '*', // eg. all languages..
43        'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
44     ), 
45
46  * 
47  */
48 require_once 'Pman.php';
49
50 class Pman_Core_I18N extends Pman
51 {
52  
53     
54      
55     
56      
57     
58     
59     function getAuth()
60     {
61         parent::getAuth(); // load company!
62         //return true;
63         $au = $this->getAuthUser();
64         //if (!$au) {
65         //    $this->jerr("Not authenticated", array('authFailure' => true));
66         //}
67         $this->authUser = $au;
68         
69         $ff= HTML_FlexyFramework::get();
70          
71         
72         $opts = empty($ff->Pman_Core_I18N) ? (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)  : $ff->Pman_Core_I18N;
73         
74         $i = DB_DataObject::Factory('I18n');
75         $this->cfg = $i->cfg();
76         
77         
78         
79         return true;
80     }
81      
82     
83      
84     function get($s ='')
85     {
86         
87         $i = DB_DataObject::Factory('I18n');
88         $i->buildDb();
89         $this->outputJavascript();
90     
91         
92         exit;
93         
94     }
95     
96     function outputJavascript()
97     {
98         
99         $i = DB_DataObject::Factory('I18n');
100         
101         $langs = $this->cfg['t'];
102        // var_dump($langs);exit;
103         $ar = array();
104         foreach($langs as $lang)
105         {
106             $rlang = array_shift(explode('_', strtoupper($lang)));
107             
108             $ar[$lang] = array(
109                 'l' => $i->toTransList('l',  $rlang),
110                 'c' => $i->toTransList('c', $rlang),
111                 'm' => $i->toTransList('m', $rlang),
112             );
113         }
114         //echo '<PRE>';print_r($ar);
115         header('Content-type: text/javascript');
116         echo 'Pman.I18n.Data = ' .  json_encode($ar);
117         exit;
118         
119         
120         
121     }
122     
123  
124     
125      /**
126      * translate (used by database building);
127      * usage :
128      * require_once 'Pman/Core/I18N.php';
129      * $x = new Pman_Core_I18N();
130      * $x->translate($this->authuser, 'c', 'US');
131      * @param au - auth User
132      * @param type = 'c' or 'l'
133      * @param k - key to translate
134      * 
135      */
136      
137     function translate($au, $type, $k) 
138     {
139       
140         static $cache;
141         if (empty($k)) {
142             return '??';
143         }
144         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
145         
146         // does it need caching?
147         
148         $i = DB_DataObject::Factory('I18n');
149         return $i->translate($lang,$type,$k);
150         
151         
152          
153         
154         
155     }
156      
157     
158 }