4 // see core_template code..
8 * Pman_Admin_Translation:
9 * - the latest version of this....
12 * output / current state: ROOT/_translations_/MODULE.js
13 * input: Pman::moduleJavascriptFilesInfo($MODULE)->translation_data
19 * Note: at present the front end does not query this to get a list of modules..
23 require_once 'Pman.php';
25 class Pman_Admin_Translations extends Pman
32 var $original = array() ; // filename => array( orig_string > orig_string)
33 var $originalKeys = array() ; // md5(filename-orig_string) => filename
38 $au = $this->getAuthUser();
40 $this->jerr("Not authenticated", array('authFailure' => true));
42 $this->authUser = $au;
47 function get($module, $opts = Array())
50 if (!empty($module)) {
52 //DB_DataObject::debugLevel(1);
53 require_once 'Services/JSON.php';
54 $d = DB_DataObject::factory('translations');
57 $d->selectAdd('distinct(tlang) as tlang');
58 header('Content-type: text/javascript');
59 $langs= $d->fetchAll('tlang');
60 foreach($langs as $lang) {
61 // output the translations strings file..
64 $this->loadOriginalStrings($module);
66 $data = $this->loadTranslateDB($lang,$module);
68 $j = new Services_JSON();
69 echo "_T.{$lang}= Roo.apply( _T.{$lang} || { }, " . $j->stringify($data, null, 4) . ");\n";
76 // load and parse json file containing all translations...
77 if (isset($_REQUEST['id'])) {
80 if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
81 $this->jerr("NO LANG / INVALID LANG");
84 $enable = $this->modulesList();
86 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
87 $this->jerr("NO MODULE / INVALID MODULE");
91 $lang = $_REQUEST['lang'];
92 $module = $_REQUEST['module'];
95 $this->loadOriginalStrings($module); // what needs translating..
97 $ff = $this->bootLoader;
98 if (empty($ff->Pman['public_cache_dir'])) {
99 $this->jerr("public_cache_dir has not been set up");
104 $translated_data = $this->loadTranslateDB($lang, $module); // the 'database!'
107 // echo '<PRE>';print_R($data);exit;
108 // covert data ready to send back..
111 foreach($this->original as $k=>$ar) {
112 foreach($ar as $tr=>$trv) {
113 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
116 'id' => $lang.'/'.$key,
120 'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
121 // 'suggest' => $hint
140 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
141 $this->jerr("NO MODULE / INVALID MODULE");
144 //id zh_HK/54e1d44609e3abed11f6e1eb6ae54988
146 list($lang,$id) = explode('/', $_REQUEST['id']);
148 $this->loadOriginalStrings($_REQUEST['module']);
150 $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
152 $data[$id] = $_REQUEST['txt'];
154 if (!isset($this->originalKeys[$id])) {
157 $this->jerr("invalid key ?");
160 $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
164 $this->writeTransMod($lang,$_REQUEST['module'], $data);
165 // write merged file..
166 //$this->prefix = '_T["'.$lang .'"]=';
167 //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
172 * load strings that need translating..
175 function loadOriginalStrings($module)
177 // since this can handle errors better.!!?
178 $info = $this->moduleJavascriptFilesInfo($module);
183 $this->original = array();
184 $tfile = $info->basedir . '/'. $info->translation_data;
186 //if (empty($tfile) || !file_exists($tfile)) {
188 foreach($info->filesmtime as $f =>$mt) {
189 $bjs = preg_replace('/\.js$/','.bjs', $f);
190 if (!file_exists($bjs)) {
193 $jd = json_decode(file_get_contents($bjs));
194 if (empty($jd->strings)) {
197 foreach($jd->strings as $tkey => $val) {
200 'tfile' => preg_replace('/\.js$/','', basename($bjs)),
206 $this->original[str_replace('.bjs', '', basename($bjs)) ] = array_flip((array)$jd->strings);
209 file_put_contents($tfile, json_encode($this->original));
215 print_R($this->original);exit;
218 $this->original = (array) json_decode( file_get_contents($tfile) );
219 ksort($this->original);
221 $this->originalKeys = array();
224 foreach($this->original as $k=>$ar) {
225 foreach($ar as $tr=>$trv) {
226 $key = md5($k.'-'.$tr);
227 $this->originalKeys[$key] = $k;
242 * @return key=>value list of translation_id=>tranlation.
247 function loadTranslateDB($lang, $module)
250 //DB_DataObject::debugLevel(1);
251 $d = DB_DataObject::factory('translations');
252 $d->module = $module;
254 $d->whereAdd('LENGTH(tval) > 0');
258 // since key includes file
259 $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
261 // no data is contained in the database, we should initialize it, if we can
262 $info = $this->moduleJavascriptFilesInfo($module);
263 $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
266 if (!file_exists($fn)) {
267 ///Die($fn ." does not exist?");
272 $default = (array) json_decode(file_get_contents($fn));
273 //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
277 foreach($default as $k=>$v) {
278 if (isset($ret[$k])) {
279 continue; // skip database already holds a version of this translation.
281 // is it relivant anymore..
282 if (!isset($this->originalKeys[$k])) {
287 $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
297 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
299 $d = DB_DataObject::factory('translations');
300 $d->module = $module;
304 if ($d->find(true)) {
316 function getTransFilename($lang, $module)
319 $ff = HTML_FlexyFramework::get();
320 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
321 if (!file_exists(dirname($fn))) {
323 /// create the direct
324 $oldumask = umask(0);
325 mkdir(dirname($fn), 0770, true);
328 if (!file_exists(dirname($fn))) {
329 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
338 * Writes a file MODULE.js inside of _translations_
340 * this should contain all contents from all the language directroris for this
345 function writeTransMod($lang, $module, $data)
350 $fn = $this->getTransFilename($lang, $module);
351 require_once 'Services/JSON.php';
352 $j = new Services_JSON();
354 file_put_contents($fn, $j->stringify($data, null, 4));
356 $ff = HTML_FlexyFramework::get();
357 $base = $ff->rootDir.'/_translations_' ;
359 foreach(scandir($base) as $l) {
361 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
364 // decode as our temp files contain spaces..
365 $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
366 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
370 file_put_contents("$base/$module.js", $out);
372 //$this->writeTrans($lang);