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;
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 $langs= $d->fetchAll('tlang');
56 foreach($langs as $lang) {
57 // output the translations strings file..
60 $this->loadOriginalStrings($module);
62 $data = $this->loadTranslateDB($lang,$module);
64 $j = new Services_JSON();
65 echo "_T.{$lang}= Roo.apply( _T.{$lang} || { }, " . $j->stringify($data, null, 4) . ");\n";
72 // load and parse json file containing all translations...
73 if (isset($_REQUEST['id'])) {
76 if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
77 $this->jerr("NO LANG / INVALID LANG");
80 $enable = $this->modulesList();
82 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
83 $this->jerr("NO MODULE / INVALID MODULE");
87 $lang = $_REQUEST['lang'];
88 $module = $_REQUEST['module'];
91 $this->loadOriginalStrings($module); // what needs translating..
93 $ff = $this->bootLoader;
94 if (empty($ff->Pman['public_cache_dir'])) {
95 $this->jerr("public_cache_dir has not been set up");
100 $translated_data = $this->loadTranslateDB($lang, $module); // the 'database!'
103 // echo '<PRE>';print_R($data);exit;
104 // covert data ready to send back..
107 foreach($this->original as $k=>$ar) {
108 foreach($ar as $tr=>$trv) {
109 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
110 $key = md5($k.'-'.$tr);
112 'id' => $lang.'/'.$key,
116 'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
117 // 'suggest' => $hint
136 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
137 $this->jerr("NO MODULE / INVALID MODULE");
140 //id zh_HK/54e1d44609e3abed11f6e1eb6ae54988
142 list($lang,$id) = explode('/', $_REQUEST['id']);
144 $this->loadOriginalStrings($_REQUEST['module']);
146 $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
148 $data[$id] = $_REQUEST['txt'];
150 if (!isset($this->originalKeys[$id])) {
153 $this->jerr("invalid key ?");
156 $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
160 $this->writeTransMod($lang,$_REQUEST['module'], $data);
161 // write merged file..
162 //$this->prefix = '_T["'.$lang .'"]=';
163 //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
168 * load strings that need translating..
171 function loadOriginalStrings($module)
173 // since this can handle errors better.!!?
174 $info = $this->moduleJavascriptFilesInfo($module);
176 $tfile =$info->basedir . '/'. $info->translation_data;
178 if (empty($tfile) || !file_exists($tfile)) {
183 require_once 'Services/JSON.php';
184 $j = new Services_JSON();
185 $this->original = (array) $j->decode('{'. file_get_contents($tfile).'}');
186 ksort($this->original);
188 $this->originalKeys = array();
191 foreach($this->original as $k=>$ar) {
192 foreach($ar as $tr=>$trv) {
193 $key = md5($k.'-'.$tr);
194 $this->originalKeys[$key] = $k;
209 * @return key=>value list of translation_id=>tranlation.
214 function loadTranslateDB($lang, $module)
217 //DB_DataObject::debugLevel(1);
218 $d = DB_DataObject::factory('translations');
219 $d->module = $module;
221 $d->whereAdd('LENGTH(tval) > 0');
225 // since key includes file
226 $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
228 // no data is contained in the database, we should initialize it, if we can
229 $info = $this->moduleJavascriptFilesInfo($module);
230 $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
233 if (!file_exists($fn)) {
234 ///Die($fn ." does not exist?");
239 $default = (array) json_decode(file_get_contents($fn));
240 //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
244 foreach($default as $k=>$v) {
245 if (isset($ret[$k])) {
246 continue; // skip database already holds a version of this translation.
248 // is it relivant anymore..
249 if (!isset($this->originalKeys[$k])) {
254 $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
264 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
266 $d = DB_DataObject::factory('translations');
267 $d->module = $module;
271 if ($d->find(true)) {
283 function getTransFilename($lang, $module)
286 $ff = HTML_FlexyFramework::get();
287 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
288 if (!file_exists(dirname($fn))) {
290 /// create the direct
291 $oldumask = umask(0);
292 mkdir(dirname($fn), 0770, true);
295 if (!file_exists(dirname($fn))) {
296 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
305 * Writes a file MODULE.js inside of _translations_
307 * this should contain all contents from all the language directroris for this
312 function writeTransMod($lang, $module, $data)
317 $fn = $this->getTransFilename($lang, $module);
318 require_once 'Services/JSON.php';
319 $j = new Services_JSON();
321 file_put_contents($fn, $j->stringify($data, null, 4));
323 $ff = HTML_FlexyFramework::get();
324 $base = $ff->rootDir.'/_translations_' ;
326 foreach(scandir($base) as $l) {
328 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
331 // decode as our temp files contain spaces..
332 $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
333 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
337 file_put_contents("$base/$module.js", $out);
339 //$this->writeTrans($lang);