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_InterfaceTranslations extends Pman
28 var $original = array() ; // filename => array( orig_string > orig_string)
29 var $originalKeys = array() ; // md5(filename-orig_string) => filename
38 function get($module, $opts = Array())
42 $this->jerr("no module selected");
45 //DB_DataObject::debugLevel(1);
46 require_once 'Services/JSON.php';
47 $d = DB_DataObject::factory('translations');
50 $d->selectAdd('distinct(tlang) as tlang');
51 header('Content-type: text/javascript');
52 $langs= $d->fetchAll('tlang');
53 foreach($langs as $lang) {
54 // output the translations strings file..
57 $this->loadOriginalStrings($module);
59 $data = $this->loadTranslateDB($lang,$module);
61 $j = new Services_JSON();
62 echo "_T = _T || {} ; _T.{$lang}= Roo.apply( _T.{$lang} || { }, " . $j->stringify($data, null, 4) . ");\n";
71 $this->jerr("invalid url");
76 * load strings that need translating..
79 function loadOriginalStrings($module)
81 // since this can handle errors better.!!?
82 $info = $this->moduleJavascriptFilesInfo($module);
87 $this->original = array();
88 $tfile = $info->basedir . '/'. $info->translation_data;
90 //if (empty($tfile) || !file_exists($tfile)) {
92 foreach($info->filesmtime as $f =>$mt) {
93 $bjs = preg_replace('/\.js$/','.bjs', $f);
94 if (!file_exists($bjs)) {
97 $jd = json_decode(file_get_contents($bjs));
98 if (empty($jd->strings)) {
101 $this->original[str_replace('.bjs', '', basename($bjs)) ] = array_flip((array)$jd->strings);
104 file_put_contents($tfile, json_encode($this->original));
110 //print_R($this->original);exit;
113 $this->original = (array) json_decode( file_get_contents($tfile) );
114 ksort($this->original);
116 $this->originalKeys = array();
119 foreach($this->original as $k=>$ar) {
120 foreach($ar as $tr=>$trv) {
121 $key = md5($k.'-'.$tr);
122 $this->originalKeys[$key] = $k;
133 * @return key=>value list of translation_id=>tranlation.
138 function loadTranslateDB($lang, $module)
141 //DB_DataObject::debugLevel(1);
142 $d = DB_DataObject::factory('translations');
143 $d->module = $module;
145 $d->whereAdd('LENGTH(tval) > 0');
149 // since key includes file
150 $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
152 // no data is contained in the database, we should initialize it, if we can
153 $info = $this->moduleJavascriptFilesInfo($module);
154 $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
157 if (!file_exists($fn)) {
158 ///Die($fn ." does not exist?");
163 $default = (array) json_decode(file_get_contents($fn));
164 //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
168 foreach($default as $k=>$v) {
169 if (isset($ret[$k])) {
170 continue; // skip database already holds a version of this translation.
172 // is it relivant anymore..
173 if (!isset($this->originalKeys[$k])) {
178 $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);