5 * Pman_Admin_Translation:
6 * - the latest version of this....
9 * output / current state: ROOT/_translations_/MODULE.js
10 * input: Pman::moduleJavascriptFilesInfo($MODULE)->translation_data
16 * Note: at present the front end does not query this to get a list of modules..
20 require_once 'Pman.php';
22 class Pman_Admin_Translations extends Pman
29 var $original = array() ; // filename => array( orig_string > orig_string)
30 var $originalKeys = array() ; // md5(filename-orig_string) => filename
35 $au = $this->getAuthUser();
37 $this->jerr("Not authenticated", array('authFailure' => true));
39 $this->authUser = $au;
44 function get($module, $opts = Array())
47 if (!empty($module)) {
49 //DB_DataObject::debugLevel(1);
50 require_once 'Services/JSON.php';
51 $d = DB_DataObject::factory('translations');
54 $d->selectAdd('distinct(tlang) as tlang');
55 header('Content-type: text/javascript');
56 $langs= $d->fetchAll('tlang');
57 foreach($langs as $lang) {
58 // output the translations strings file..
61 $this->loadOriginalStrings($module);
63 $data = $this->loadTranslateDB($lang,$module);
65 $j = new Services_JSON();
66 echo "_T.{$lang}= Roo.apply( _T.{$lang} || { }, " . $j->stringify($data, null, 4) . ");\n";
73 // load and parse json file containing all translations...
74 if (isset($_REQUEST['id'])) {
77 if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
78 $this->jerr("NO LANG / INVALID LANG");
81 $enable = $this->modulesList();
83 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
84 $this->jerr("NO MODULE / INVALID MODULE");
88 $lang = $_REQUEST['lang'];
89 $module = $_REQUEST['module'];
92 $this->loadOriginalStrings($module); // what needs translating..
94 $ff = $this->bootLoader;
95 if (empty($ff->Pman['public_cache_dir'])) {
96 $this->jerr("public_cache_dir has not been set up");
101 $translated_data = $this->loadTranslateDB($lang, $module); // the 'database!'
104 // echo '<PRE>';print_R($data);exit;
105 // covert data ready to send back..
108 foreach($this->original as $k=>$ar) {
109 foreach($ar as $tr=>$trv) {
110 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
111 $key = md5($k.'-'.$tr);
113 'id' => $lang.'/'.$key,
117 'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
118 // 'suggest' => $hint
137 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
138 $this->jerr("NO MODULE / INVALID MODULE");
141 //id zh_HK/54e1d44609e3abed11f6e1eb6ae54988
143 list($lang,$id) = explode('/', $_REQUEST['id']);
145 $this->loadOriginalStrings($_REQUEST['module']);
147 $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
149 $data[$id] = $_REQUEST['txt'];
151 if (!isset($this->originalKeys[$id])) {
154 $this->jerr("invalid key ?");
157 $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
161 $this->writeTransMod($lang,$_REQUEST['module'], $data);
162 // write merged file..
163 //$this->prefix = '_T["'.$lang .'"]=';
164 //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
169 * load strings that need translating..
172 function loadOriginalStrings($module)
174 // since this can handle errors better.!!?
175 $info = $this->moduleJavascriptFilesInfo($module);
177 $tfile =$info->basedir . '/'. $info->translation_data;
179 if (empty($tfile) || !file_exists($tfile)) {
184 require_once 'Services/JSON.php';
185 $j = new Services_JSON();
186 $this->original = (array) $j->decode('{'. file_get_contents($tfile).'}');
187 ksort($this->original);
189 $this->originalKeys = array();
192 foreach($this->original as $k=>$ar) {
193 foreach($ar as $tr=>$trv) {
194 $key = md5($k.'-'.$tr);
195 $this->originalKeys[$key] = $k;
210 * @return key=>value list of translation_id=>tranlation.
215 function loadTranslateDB($lang, $module)
218 //DB_DataObject::debugLevel(1);
219 $d = DB_DataObject::factory('translations');
220 $d->module = $module;
222 $d->whereAdd('LENGTH(tval) > 0');
226 // since key includes file
227 $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
229 // no data is contained in the database, we should initialize it, if we can
230 $info = $this->moduleJavascriptFilesInfo($module);
231 $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
234 if (!file_exists($fn)) {
235 ///Die($fn ." does not exist?");
240 $default = (array) json_decode(file_get_contents($fn));
241 //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
245 foreach($default as $k=>$v) {
246 if (isset($ret[$k])) {
247 continue; // skip database already holds a version of this translation.
249 // is it relivant anymore..
250 if (!isset($this->originalKeys[$k])) {
255 $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
265 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
267 $d = DB_DataObject::factory('translations');
268 $d->module = $module;
272 if ($d->find(true)) {
284 function getTransFilename($lang, $module)
287 $ff = HTML_FlexyFramework::get();
288 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
289 if (!file_exists(dirname($fn))) {
291 /// create the direct
292 $oldumask = umask(0);
293 mkdir(dirname($fn), 0770, true);
296 if (!file_exists(dirname($fn))) {
297 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
306 * Writes a file MODULE.js inside of _translations_
308 * this should contain all contents from all the language directroris for this
313 function writeTransMod($lang, $module, $data)
318 $fn = $this->getTransFilename($lang, $module);
319 require_once 'Services/JSON.php';
320 $j = new Services_JSON();
322 file_put_contents($fn, $j->stringify($data, null, 4));
324 $ff = HTML_FlexyFramework::get();
325 $base = $ff->rootDir.'/_translations_' ;
327 foreach(scandir($base) as $l) {
329 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
332 // decode as our temp files contain spaces..
333 $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
334 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
338 file_put_contents("$base/$module.js", $out);
340 //$this->writeTrans($lang);