typo
[Pman.Admin] / Translations.php
index 0c96ba1..a0c8aec 100644 (file)
@@ -1,5 +1,8 @@
 <?php
 
+die("NOT USED!?");
+// see core_template code..
+
 
 /**
  * Pman_Admin_Translation:
@@ -26,6 +29,8 @@ class Pman_Admin_Translations extends Pman
     var $fn = '';
     var $data = array();
     
+    var $original = array() ; // filename => array( orig_string > orig_string)
+    var $originalKeys = array() ; // md5(filename-orig_string) => filename
     
     function getAuth()
     {
@@ -39,8 +44,35 @@ class Pman_Admin_Translations extends Pman
     }
     
     
-    function get()
+    function get($module, $opts = Array())
     {
+        
+        if (!empty($module)) {
+            $this->init();
+            //DB_DataObject::debugLevel(1);
+            require_once 'Services/JSON.php';
+            $d = DB_DataObject::factory('translations');
+            $d->module = $module;
+            $d->selectAdd();
+            $d->selectAdd('distinct(tlang) as tlang');
+            header('Content-type: text/javascript');
+            $langs= $d->fetchAll('tlang');
+            foreach($langs as $lang) {
+                // output the translations strings file..
+                
+                    
+                $this->loadOriginalStrings($module);
+                
+                $data = $this->loadTranslateDB($lang,$module);
+                
+                $j = new Services_JSON();
+                echo "_T.{$lang}= Roo.apply( _T.{$lang} || { }, " .  $j->stringify($data, null, 4) . ");\n";
+                
+            }
+            exit;
+            
+        }
+        
         // load and parse json file containing all translations...
         if (isset($_REQUEST['id'])) {
             return $this->post();
@@ -60,25 +92,26 @@ class Pman_Admin_Translations extends Pman
         $module = $_REQUEST['module'];
         
         
-        $data = $this->loadOriginalStrings($lang,$module); // what needs translating..
+        $this->loadOriginalStrings($module); // what needs translating..
         
-        $translated_data = $this->loadTranslate($lang, $module); // the 'database!'
+        $ff = $this->bootLoader;
+        if (empty($ff->Pman['public_cache_dir'])) {
+            $this->jerr("public_cache_dir has not been set up");
+        }
         
         
         
-        // overlay translated..
-        if (empty($translated_data)) {
-            // try using old data!!!!
-            $translated_data = $this->loadOld($lang,$data); // phase out???
-                    
-        }
+        $translated_data = $this->loadTranslateDB($lang, $module); // the 'database!'
+        
+        
+       // echo '<PRE>';print_R($data);exit;
         // covert data ready to send back..
         
         $ret = array();
-        foreach($data as $k=>$ar) {
+        foreach($this->original as $k=>$ar) {
             foreach($ar as $tr=>$trv) {
                 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
-                $key = md5($k.'-'.$tr);
+                $key = $trv;
                 $ret[] = array(
                     'id' => $lang.'/'.$key,
                     'msum' => $key,
@@ -99,12 +132,12 @@ class Pman_Admin_Translations extends Pman
         
     }
     
-    function post() 
+    function post($v
     {
          
-        $fm = HTML_FlexyFramework::get();
-        $enable = explode(',',   $fm->enable);
-        if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
+         
+        
+        if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
             $this->jerr("NO MODULE / INVALID MODULE");
         }
         
@@ -112,15 +145,21 @@ class Pman_Admin_Translations extends Pman
         //txt  é \85ç\9b®
         list($lang,$id) = explode('/', $_REQUEST['id']);
         
+        $this->loadOriginalStrings($_REQUEST['module']);
+        
+        $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
         
-        $data = $this->loadTranslate($lang,$_REQUEST['module']);
-        if (empty($data)) {
-            // try using old data!!!!
-            $data = $this->loadOld($lang,$data); // phase out???
-                    
-        }
         $data[$id] = $_REQUEST['txt'];
         
+        if (!isset($this->originalKeys[$id])) {
+            
+            
+            $this->jerr("invalid key ?");
+        }
+        
+        $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
+        
+        
         
         $this->writeTransMod($lang,$_REQUEST['module'], $data);
         // write merged file..
@@ -133,68 +172,133 @@ class Pman_Admin_Translations extends Pman
      * load strings that need translating..
      */
     
-    function loadOriginalStrings($lang, $module)
+    function loadOriginalStrings($module)
     {
         // since this can handle errors better.!!?
         $info = $this->moduleJavascriptFilesInfo($module);
         //print_r($info);
-        $tfile =$info->translation_base . $info->translation_data;
         
-        if (empty($tfile) || !file_exists($tfile)) {
-            return array();
-        }
         
         
-        require_once 'Services/JSON.php';
-        $j = new Services_JSON();
+        $this->original  = array();
+        $tfile = $info->basedir . '/'. $info->translation_data;
+         //var_dump($tfile);
+        //if (empty($tfile) || !file_exists($tfile)) {
+            
+            foreach($info->filesmtime as $f =>$mt) {
+                $bjs = preg_replace('/\.js$/','.bjs', $f);
+                if (!file_exists($bjs)) {
+                    continue;
+                }
+                $jd = json_decode(file_get_contents($bjs));
+                if (empty($jd->strings)) {
+                    continue;
+                }
+                foreach($jd->strings as $tkey => $val) {
+                    $line = array(
+                        'module' => $module,
+                        'tfile' => preg_replace('/\.js$/','',  basename($bjs)),
+                    );
+                    
+                    
+                }
+                
+                $this->original[str_replace('.bjs', '', basename($bjs)) ] = array_flip((array)$jd->strings);
+            }
+             
+            file_put_contents($tfile, json_encode($this->original));
+            
+            
+        //}
+        
+        
+        print_R($this->original);exit;
+        
+        
+        $this->original = (array) json_decode( file_get_contents($tfile) );
+        ksort($this->original);
+
+        $this->originalKeys = array();
+        
+        // 
+        foreach($this->original as $k=>$ar) {
+            foreach($ar as $tr=>$trv) {
+                $key = md5($k.'-'.$tr);
+                $this->originalKeys[$key] = $k;
+            }
+        }
         
-        return (array) $j->decode('{'. file_get_contents($tfile).'}');
     }
     
      
     
     
-    /**
-     * 
-     * Load the user translated strings.
-     * 
+   
+    /***
+     *
+     * loadTranslateDB -
+     *
+     *
+     * @return key=>value list of translation_id=>tranlation.
+     *
+     *
      */
-    function loadTranslate($lang, $module)
+    
+    function loadTranslateDB($lang, $module)
     {
-         
-        $fn = $this->getTransFilename($lang,$module);
         
-         
+        //DB_DataObject::debugLevel(1);
+        $d = DB_DataObject::factory('translations');
+        $d->module = $module;
+        $d->tlang = $lang;
+        $d->whereAdd('LENGTH(tval) > 0');
+        $ret = array();
+        
+        if ($d->count()) {
+            // since key includes file 
+            $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
+        }
+        // no data is contained in the database, we should initialize it, if we can
+        $info  = $this->moduleJavascriptFilesInfo($module);
+        $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
+        
+       
         if (!file_exists($fn)) {
-            return array();
+            ///Die($fn ." does not exist?");
+            return $ret;
         }
         
-        return (array) json_decode(file_get_contents($fn));
-        //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
-    }
-    
-    
-    function loadTranslateDB($lang, $module)
-    {
-        $d = DB_DataObject('translations');
-        $d->module = $module;
-        $d->lang = $lang;
-        $d->find();
-        while ($d->fetch()) {
-            if (!isset($ret[$d->tfile])) {
-                $ret[$d->tfile] = array( $d->tkey => $d->tval );
+        
+        $default = (array) json_decode(file_get_contents($fn));
+        //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
+        
+        
+        
+        foreach($default as $k=>$v) {
+            if (isset($ret[$k])) {
+                continue; // skip database already holds a version of this translation.
+            }
+            // is it relivant anymore..
+            if (!isset($this->originalKeys[$k])) {
                 continue;
             }
-            $ret[$d->tfile][$d->tkey] = $d->tval;
+            
+            // it's current..
+            $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
+            $ret[$k] = $v;
+            
+            
         }
         return $ret;
+        
+         
     }
     
     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
     {
-        $d = DB_DataObject('translations');
+        $d = DB_DataObject::factory('translations');
         $d->module = $module;
-        $d->lang = $lang;
+        $d->tlang = $lang;
         $d->tfile = $tfile;
         $d->tkey = $tkey;
         if ($d->find(true)) {
@@ -229,49 +333,27 @@ class Pman_Admin_Translations extends Pman
         return $fn;
     }
     
-    
-  
-    function loadOld($lang,$data)
-    {
-        // need the old for hinting..
-        
-        // this is ok - as it's inside this module.
-        $old = (array) json_decode(file_get_contents(dirname(__FILE__).'/data/oldeng.js'));
-        //print_r($old);
-        // contains key/value of data..
-        $prefix = '_T["'.$lang .'"]=';
-        $trans = (array) json_decode(substr(file_get_contents(
-            dirname(__FILE__).'/data/lang.' . $lang . '.js'
-        ), strlen($prefix), -1));
-        
-       // echo '<PRE>';print_r($trans);
-        
-        $hints = array();
-        foreach($old as $k=>$v) {
-            if (isset($trans[$k]) && empty($hints[$v])) {
-                $hints[$v] = $trans[$k];
-            }
-        }
-        $translated_data = array();
-        foreach($data as $k=>$ar) {
-                foreach($ar as $tr=>$trv) {
-                $key = md5($k.'-'.$tr);
-                if (isset($hints[$tr])) {
-                    $translated_data[$key] = $hints[$tr];
-                }
-            }
-        }
-        // $this->writeTransMod($lang, $module, $translated_data);
-        //echo '<PRE>';print_r($hints);
-        return $translated_data;
-        
-    }
+     
+    /**
+     * Writes a file MODULE.js inside of _translations_
+     *
+     * this should contain all contents from all the language directroris for this
+     * module
+     *
+     */
     
     function writeTransMod($lang, $module, $data)
     {
+        //print_R($data);
+        
+        
         $fn = $this->getTransFilename($lang, $module);
-        file_put_contents($fn, json_encode($data));
-         $ff = HTML_FlexyFramework::get();
+        require_once 'Services/JSON.php';
+        $j = new Services_JSON();
+        
+        file_put_contents($fn, $j->stringify($data, null, 4));
+        
+        $ff = HTML_FlexyFramework::get();
         $base = $ff->rootDir.'/_translations_' ;
         $out = '';
         foreach(scandir($base) as $l) {
@@ -279,8 +361,9 @@ class Pman_Admin_Translations extends Pman
             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
                 continue;
             }
-              
-            $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
+            // decode as our temp files contain spaces..
+            $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
+            $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
         }
         //var_dump($out);
         if (strlen($out)) {
@@ -289,6 +372,6 @@ class Pman_Admin_Translations extends Pman
         //$this->writeTrans($lang);
     }
      
-    
+     
     
 }
\ No newline at end of file