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;
46 // load and parse json file containing all translations...
47 if (isset($_REQUEST['id'])) {
50 if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
51 $this->jerr("NO LANG / INVALID LANG");
54 $enable = $this->modulesList();
56 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
57 $this->jerr("NO MODULE / INVALID MODULE");
61 $lang = $_REQUEST['lang'];
62 $module = $_REQUEST['module'];
65 $this->loadOriginalStrings($module); // what needs translating..
72 $translated_data = $this->loadTranslateDB($lang, $module); // the 'database!'
75 // echo '<PRE>';print_R($data);exit;
76 // covert data ready to send back..
79 foreach($this->original as $k=>$ar) {
80 foreach($ar as $tr=>$trv) {
81 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
82 $key = md5($k.'-'.$tr);
84 'id' => $lang.'/'.$key,
88 'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
108 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
109 $this->jerr("NO MODULE / INVALID MODULE");
112 //id zh_HK/54e1d44609e3abed11f6e1eb6ae54988
114 list($lang,$id) = explode('/', $_REQUEST['id']);
116 $this->loadOriginalStrings($_REQUEST['module']);
118 $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
120 $data[$id] = $_REQUEST['txt'];
122 if (!isset($this->originalKeys[$id])) {
125 $this->jerr("invalid key ?");
128 $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
132 $this->writeTransMod($lang,$_REQUEST['module'], $data);
133 // write merged file..
134 //$this->prefix = '_T["'.$lang .'"]=';
135 //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
140 * load strings that need translating..
143 function loadOriginalStrings($module)
145 // since this can handle errors better.!!?
146 $info = $this->moduleJavascriptFilesInfo($module);
148 $tfile =$info->basedir . '/'. $info->translation_data;
150 if (empty($tfile) || !file_exists($tfile)) {
155 require_once 'Services/JSON.php';
156 $j = new Services_JSON();
157 $this->original = (array) $j->decode('{'. file_get_contents($tfile).'}');
158 ksort($this->original);
160 $this->originalKeys = array();
163 foreach($this->original as $k=>$ar) {
164 foreach($ar as $tr=>$trv) {
165 $key = md5($k.'-'.$tr);
166 $this->originalKeys[$key] = $k;
181 * @return key=>value list of translation_id=>tranlation.
186 function loadTranslateDB($lang, $module)
189 //DB_DataObject::debugLevel(1);
190 $d = DB_DataObject::factory('translations');
191 $d->module = $module;
196 // since key includes file
197 $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
199 // no data is contained in the database, we should initialize it, if we can
200 $info = $this->moduleJavascriptFilesInfo($module);
201 $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
204 if (!file_exists($fn)) {
205 ///Die($fn ." does not exist?");
210 $default = (array) json_decode(file_get_contents($fn));
211 //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
215 foreach($default as $k=>$v) {
216 if (isset($ret[$k])) {
217 continue; // skip database already holds a version of this translation.
219 // is it relivant anymore..
220 if (!isset($this->originalKeys[$k])) {
224 $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
234 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
236 $d = DB_DataObject::factory('translations');
237 $d->module = $module;
241 if ($d->find(true)) {
253 function getTransFilename($lang, $module)
256 $ff = HTML_FlexyFramework::get();
257 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
258 if (!file_exists(dirname($fn))) {
260 /// create the direct
261 $oldumask = umask(0);
262 mkdir(dirname($fn), 0770, true);
265 if (!file_exists(dirname($fn))) {
266 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
275 * Writes a file MODULE.js inside of _translations_
277 * this should contain all contents from all the language directroris for this
282 function writeTransMod($lang, $module, $data)
287 $fn = $this->getTransFilename($lang, $module);
288 require_once 'Services/JSON.php';
289 $j = new Services_JSON();
291 file_put_contents($fn, $j->stringify($data, null, 4));
293 $ff = HTML_FlexyFramework::get();
294 $base = $ff->rootDir.'/_translations_' ;
296 foreach(scandir($base) as $l) {
298 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
301 // decode as our temp files contain spaces..
302 $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
303 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
307 file_put_contents("$base/$module.js", $out);
309 //$this->writeTrans($lang);