Translations.php
[Pman.Admin] / Translations.php
1 <?php
2
3
4 /**
5  * Pman_Admin_Translation:
6  * - the latest version of this....
7  *
8  * Files:
9  *   output / current state:    ROOT/_translations_/MODULE.js
10  *   input:       Pman::moduleJavascriptFilesInfo($MODULE)->translation_data
11  * 
12  * 
13  * see:
14  * Pman->modulesList()
15  *
16  * Note: at present the front end does not query this to get a list of modules..
17  * 
18  */
19
20 require_once 'Pman.php';
21
22 class Pman_Admin_Translations extends Pman
23 {
24     
25     var $prefix = '';
26     var $fn = '';
27     var $data = array();
28     
29     
30     function getAuth()
31     {
32         parent::getAuth();
33         $au = $this->getAuthUser();
34         if (!$au) {
35             $this->jerr("Not authenticated", array('authFailure' => true));
36         }
37         $this->authUser = $au;
38         return true;
39     }
40     
41     
42     function get()
43     {
44         // load and parse json file containing all translations...
45         if (isset($_REQUEST['id'])) {
46             return $this->post();
47         }
48         if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
49             $this->jerr("NO LANG / INVALID LANG");
50         }
51          
52         $enable = $this->modulesList();
53         
54         if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
55             $this->jerr("NO MODULE / INVALID MODULE");
56         }
57         
58         
59         $lang = $_REQUEST['lang'];
60         $module = $_REQUEST['module'];
61         
62         
63         $data = $this->loadOriginalStrings($lang,$module); // what needs translating..
64         
65         $translated_data = $this->loadTranslate($lang, $module); // the 'database!'
66         
67         
68         
69         // overlay translated..
70         if (empty($translated_data)) {
71             // try using old data!!!!
72             $translated_data = $this->loadOld($lang,$data); // phase out???
73                     
74         }
75         // covert data ready to send back..
76         
77         $ret = array();
78         foreach($data as $k=>$ar) {
79             foreach($ar as $tr=>$trv) {
80                 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
81                 $key = md5($k.'-'.$tr);
82                 $ret[] = array(
83                     'id' => $lang.'/'.$key,
84                     'msum' => $key,
85                     'colname' => $k,
86                     'origtxt' => $tr,
87                     'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
88                    // 'suggest' => $hint
89                 );
90                 
91             }
92         }
93         
94         
95         $this->jdata($ret);
96         
97         
98         exit;
99         
100     }
101     
102     function post() 
103     {
104          
105         $fm = HTML_FlexyFramework::get();
106         $enable = explode(',',   $fm->enable);
107         if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
108             $this->jerr("NO MODULE / INVALID MODULE");
109         }
110         
111         //id    zh_HK/54e1d44609e3abed11f6e1eb6ae54988
112         //txt   é \85ç\9b®
113         list($lang,$id) = explode('/', $_REQUEST['id']);
114         
115         
116         $data = $this->loadTranslate($lang,$_REQUEST['module']);
117         if (empty($data)) {
118             // try using old data!!!!
119             $data = $this->loadOld($lang,$data); // phase out???
120                     
121         }
122         $data[$id] = $_REQUEST['txt'];
123         
124         
125         $this->writeTransMod($lang,$_REQUEST['module'], $data);
126         // write merged file..
127         //$this->prefix = '_T["'.$lang .'"]=';
128         //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
129         $this->jok("OK");
130     }
131     
132     /**
133      * load strings that need translating..
134      */
135     
136     function loadOriginalStrings($lang, $module)
137     {
138         // since this can handle errors better.!!?
139         require_once 'Services/JSON.php';
140         $j = new Services_JSON();
141         
142         $ff = HTML_FlexyFramework::get();
143         $trans = file_get_contents(
144             $ff->rootDir . '/Pman/' . $module. "/compiled/_translation_.js");
145         
146         
147         return (array) $j->decode('{'. $trans .'}');
148     }
149     
150      
151     
152     
153     /**
154      * 
155      * Load the user translated strings.
156      * 
157      */
158     function loadTranslate($lang, $module)
159     {
160          
161         $fn = $this->getTransFilename($lang,$module);
162         
163          
164         if (!file_exists($fn)) {
165             return array();
166         }
167         
168         return (array) json_decode(file_get_contents($fn));
169         //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
170     }
171     
172     
173     function loadTranslateDB($lang, $module)
174     {
175         $d = DB_DataObject('translations');
176         $d->module = $module;
177         $d->lang = $lang;
178         $d->find();
179         while ($d->fetch()) {
180             if (!isset($ret[$d->tfile])) {
181                 $ret[$d->tfile] = array( $d->tkey => $d->tval );
182                 continue;
183             }
184             $ret[$d->tfile][$d->tkey] = $d->tval;
185         }
186         return $ret;
187     }
188     
189     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
190     {
191         $d = DB_DataObject('translations');
192         $d->module = $module;
193         $d->lang = $lang;
194         $d->tfile = $tfile;
195         $d->tkey = $tkey;
196         if ($d->find(true)) {
197             $d->tval = $tval;
198             $d->update();
199         }
200         $d->tval = $tval;
201         $d->insert();
202         
203     }
204     
205     
206     
207     
208     function getTransFilename($lang, $module)
209     {
210         
211         $ff = HTML_FlexyFramework::get();
212         $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
213         if (!file_exists(dirname($fn))) {
214             
215             /// create the direct
216             $oldumask = umask(0);
217             mkdir(dirname($fn), 0770, true);
218             umask($oldumask);  
219             clearstatcache();
220             if (!file_exists(dirname($fn))) {
221                 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
222             }
223             
224         }
225         return $fn;
226     }
227     
228     
229   
230     function loadOld($lang,$data)
231     {
232         // need the old for hinting..
233         
234         // this is ok - as it's inside this module.
235         $old = (array) json_decode(file_get_contents(dirname(__FILE__).'/data/oldeng.js'));
236         //print_r($old);
237         // contains key/value of data..
238         $prefix = '_T["'.$lang .'"]=';
239         $trans = (array) json_decode(substr(file_get_contents(
240             dirname(__FILE__).'/data/lang.' . $lang . '.js'
241         ), strlen($prefix), -1));
242         
243        // echo '<PRE>';print_r($trans);
244         
245         $hints = array();
246         foreach($old as $k=>$v) {
247             if (isset($trans[$k]) && empty($hints[$v])) {
248                 $hints[$v] = $trans[$k];
249             }
250         }
251         $translated_data = array();
252         foreach($data as $k=>$ar) {
253                 foreach($ar as $tr=>$trv) {
254                 $key = md5($k.'-'.$tr);
255                 if (isset($hints[$tr])) {
256                     $translated_data[$key] = $hints[$tr];
257                 }
258             }
259         }
260         // $this->writeTransMod($lang, $module, $translated_data);
261         //echo '<PRE>';print_r($hints);
262         return $translated_data;
263         
264     }
265     
266     function writeTransMod($lang, $module, $data)
267     {
268         $fn = $this->getTransFilename($lang, $module);
269         file_put_contents($fn, json_encode($data));
270          $ff = HTML_FlexyFramework::get();
271         $base = $ff->rootDir.'/_translations_' ;
272         $out = '';
273         foreach(scandir($base) as $l) {
274              
275             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
276                 continue;
277             }
278               
279             $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
280         }
281         //var_dump($out);
282         if (strlen($out)) {
283             file_put_contents("$base/$module.js", $out);
284         }
285         //$this->writeTrans($lang);
286     }
287      
288     
289     
290 }