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/Admin/Translations.php';
22 class Pman_Admin_Translations extends Pman_Admin_Translations
29 var $original = array() ; // filename => array( orig_string > orig_string)
30 var $originalKeys = array() ; // md5(filename-orig_string) => filename
42 if (!empty($module)) {
44 //DB_DataObject::debugLevel(1);
45 require_once 'Services/JSON.php';
46 $d = DB_DataObject::factory('translations');
49 $d->selectAdd('distinct(tlang) as tlang');
50 header('Content-type: text/javascript');
51 $langs= $d->fetchAll('tlang');
52 foreach($langs as $lang) {
53 // output the translations strings file..
56 $this->loadOriginalStrings($module);
58 $data = $this->loadTranslateDB($lang,$module);
60 $j = new Services_JSON();
61 echo "_T.{$lang}= Roo.apply( _T.{$lang} || { }, " . $j->stringify($data, null, 4) . ");\n";
68 // load and parse json file containing all translations...
69 if (isset($_REQUEST['id'])) {
72 if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
73 $this->jerr("NO LANG / INVALID LANG");
76 $enable = $this->modulesList();
78 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
79 $this->jerr("NO MODULE / INVALID MODULE");
83 $lang = $_REQUEST['lang'];
84 $module = $_REQUEST['module'];
87 $this->loadOriginalStrings($module); // what needs translating..
89 $ff = $this->bootLoader;
90 if (empty($ff->Pman['public_cache_dir'])) {
91 $this->jerr("public_cache_dir has not been set up");
96 $translated_data = $this->loadTranslateDB($lang, $module); // the 'database!'
99 // echo '<PRE>';print_R($data);exit;
100 // covert data ready to send back..
103 foreach($this->original as $k=>$ar) {
104 foreach($ar as $tr=>$trv) {
105 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
106 $key = md5($k.'-'.$tr);
108 'id' => $lang.'/'.$key,
112 'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
113 // 'suggest' => $hint
132 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
133 $this->jerr("NO MODULE / INVALID MODULE");
136 //id zh_HK/54e1d44609e3abed11f6e1eb6ae54988
138 list($lang,$id) = explode('/', $_REQUEST['id']);
140 $this->loadOriginalStrings($_REQUEST['module']);
142 $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
144 $data[$id] = $_REQUEST['txt'];
146 if (!isset($this->originalKeys[$id])) {
149 $this->jerr("invalid key ?");
152 $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
156 $this->writeTransMod($lang,$_REQUEST['module'], $data);
157 // write merged file..
158 //$this->prefix = '_T["'.$lang .'"]=';
159 //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
164 * load strings that need translating..
167 function loadOriginalStrings($module)
169 // since this can handle errors better.!!?
170 $info = $this->moduleJavascriptFilesInfo($module);
172 $tfile =$info->basedir . '/'. $info->translation_data;
174 if (empty($tfile) || !file_exists($tfile)) {
179 require_once 'Services/JSON.php';
180 $j = new Services_JSON();
181 $this->original = (array) $j->decode('{'. file_get_contents($tfile).'}');
182 ksort($this->original);
184 $this->originalKeys = array();
187 foreach($this->original as $k=>$ar) {
188 foreach($ar as $tr=>$trv) {
189 $key = md5($k.'-'.$tr);
190 $this->originalKeys[$key] = $k;
205 * @return key=>value list of translation_id=>tranlation.
210 function loadTranslateDB($lang, $module)
213 //DB_DataObject::debugLevel(1);
214 $d = DB_DataObject::factory('translations');
215 $d->module = $module;
217 $d->whereAdd('LENGTH(tval) > 0');
221 // since key includes file
222 $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
224 // no data is contained in the database, we should initialize it, if we can
225 $info = $this->moduleJavascriptFilesInfo($module);
226 $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
229 if (!file_exists($fn)) {
230 ///Die($fn ." does not exist?");
235 $default = (array) json_decode(file_get_contents($fn));
236 //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
240 foreach($default as $k=>$v) {
241 if (isset($ret[$k])) {
242 continue; // skip database already holds a version of this translation.
244 // is it relivant anymore..
245 if (!isset($this->originalKeys[$k])) {
250 $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
260 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
262 $d = DB_DataObject::factory('translations');
263 $d->module = $module;
267 if ($d->find(true)) {
279 function getTransFilename($lang, $module)
282 $ff = HTML_FlexyFramework::get();
283 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
284 if (!file_exists(dirname($fn))) {
286 /// create the direct
287 $oldumask = umask(0);
288 mkdir(dirname($fn), 0770, true);
291 if (!file_exists(dirname($fn))) {
292 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
301 * Writes a file MODULE.js inside of _translations_
303 * this should contain all contents from all the language directroris for this
308 function writeTransMod($lang, $module, $data)
313 $fn = $this->getTransFilename($lang, $module);
314 require_once 'Services/JSON.php';
315 $j = new Services_JSON();
317 file_put_contents($fn, $j->stringify($data, null, 4));
319 $ff = HTML_FlexyFramework::get();
320 $base = $ff->rootDir.'/_translations_' ;
322 foreach(scandir($base) as $l) {
324 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
327 // decode as our temp files contain spaces..
328 $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
329 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
333 file_put_contents("$base/$module.js", $out);
335 //$this->writeTrans($lang);