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