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         $info = $this->moduleJavascriptFilesInfo($module);
140         //print_r($info);
141         $tfile =$info->basedir . '/'. $info->translation_data;
142         //var_dump($tfile);
143         if (empty($tfile) || !file_exists($tfile)) {
144             return array();
145         }
146         
147         
148         require_once 'Services/JSON.php';
149         $j = new Services_JSON();
150         
151         return (array) $j->decode('{'. file_get_contents($tfile).'}');
152     }
153     
154      
155     
156     
157     /**
158      * 
159      * Load the user translated strings.
160      * 
161      */
162     function loadTranslate($lang, $module)
163     {
164          
165         $fn = $this->getTransFilename($lang,$module);
166         
167          
168         if (!file_exists($fn)) {
169             return array();
170         }
171         
172         return (array) json_decode(file_get_contents($fn));
173         //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
174     }
175     
176     
177     function loadTranslateDB($lang, $module)
178     {
179         $d = DB_DataObject('translations');
180         $d->module = $module;
181         $d->lang = $lang;
182         $d->find();
183         while ($d->fetch()) {
184             if (!isset($ret[$d->tfile])) {
185                 $ret[$d->tfile] = array( $d->tkey => $d->tval );
186                 continue;
187             }
188             $ret[$d->tfile][$d->tkey] = $d->tval;
189         }
190         return $ret;
191     }
192     
193     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
194     {
195         $d = DB_DataObject('translations');
196         $d->module = $module;
197         $d->lang = $lang;
198         $d->tfile = $tfile;
199         $d->tkey = $tkey;
200         if ($d->find(true)) {
201             $d->tval = $tval;
202             $d->update();
203         }
204         $d->tval = $tval;
205         $d->insert();
206         
207     }
208     
209     
210     
211     
212     function getTransFilename($lang, $module)
213     {
214         
215         $ff = HTML_FlexyFramework::get();
216         $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
217         if (!file_exists(dirname($fn))) {
218             
219             /// create the direct
220             $oldumask = umask(0);
221             mkdir(dirname($fn), 0770, true);
222             umask($oldumask);  
223             clearstatcache();
224             if (!file_exists(dirname($fn))) {
225                 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
226             }
227             
228         }
229         return $fn;
230     }
231     
232     
233   
234     function loadOld($lang,$data)
235     {
236         // need the old for hinting..
237         
238         // this is ok - as it's inside this module.
239         $old = (array) json_decode(file_get_contents(dirname(__FILE__).'/data/oldeng.js'));
240         //print_r($old);
241         // contains key/value of data..
242         $prefix = '_T["'.$lang .'"]=';
243         $trans = (array) json_decode(substr(file_get_contents(
244             dirname(__FILE__).'/data/lang.' . $lang . '.js'
245         ), strlen($prefix), -1));
246         
247        // echo '<PRE>';print_r($trans);
248         
249         $hints = array();
250         foreach($old as $k=>$v) {
251             if (isset($trans[$k]) && empty($hints[$v])) {
252                 $hints[$v] = $trans[$k];
253             }
254         }
255         $translated_data = array();
256         foreach($data as $k=>$ar) {
257                 foreach($ar as $tr=>$trv) {
258                 $key = md5($k.'-'.$tr);
259                 if (isset($hints[$tr])) {
260                     $translated_data[$key] = $hints[$tr];
261                 }
262             }
263         }
264         // $this->writeTransMod($lang, $module, $translated_data);
265         //echo '<PRE>';print_r($hints);
266         return $translated_data;
267         
268     }
269     /**
270      * Writes a file MODULE.js inside of _translations_
271      *
272      * this should contain all contents from all the language directroris for this
273      * module
274      *
275      */
276     
277     function writeTransMod($lang, $module, $data)
278     {
279         $fn = $this->getTransFilename($lang, $module);
280         file_put_contents($fn, json_encode($data));
281          $ff = HTML_FlexyFramework::get();
282         $base = $ff->rootDir.'/_translations_' ;
283         $out = '';
284         foreach(scandir($base) as $l) {
285              
286             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
287                 continue;
288             }
289               
290             $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
291         }
292         //var_dump($out);
293         if (strlen($out)) {
294             file_put_contents("$base/$module.js", $out);
295         }
296         //$this->writeTrans($lang);
297     }
298      
299     
300     
301 }