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
33 $au = $this->getAuthUser();
35 $this->jerr("Not authenticated", array('authFailure' => true));
37 $this->authUser = $au;
44 // load and parse json file containing all translations...
45 if (isset($_REQUEST['id'])) {
48 if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
49 $this->jerr("NO LANG / INVALID LANG");
52 $enable = $this->modulesList();
54 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
55 $this->jerr("NO MODULE / INVALID MODULE");
59 $lang = $_REQUEST['lang'];
60 $module = $_REQUEST['module'];
63 $data = $this->loadOriginalStrings($lang,$module); // what needs translating..
65 $translated_data = $this->loadTranslate($lang, $module); // the 'database!'
69 // overlay translated..
70 if (empty($translated_data)) {
71 // try using old data!!!!
72 $translated_data = $this->loadOld($lang,$data); // phase out???
75 // covert data ready to send back..
78 foreach($data as $k=>$ar) {
79 foreach($ar as $tr=>$trv) {
80 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
81 $key = md5($k.'-'.$tr);
83 'id' => $lang.'/'.$key,
87 'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
105 $fm = HTML_FlexyFramework::get();
106 $enable = explode(',', $fm->enable);
107 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
108 $this->jerr("NO MODULE / INVALID MODULE");
111 //id zh_HK/54e1d44609e3abed11f6e1eb6ae54988
113 list($lang,$id) = explode('/', $_REQUEST['id']);
116 $data = $this->loadTranslate($lang,$_REQUEST['module']);
118 // try using old data!!!!
119 $data = $this->loadOld($lang,$data); // phase out???
122 $data[$id] = $_REQUEST['txt'];
125 $this->writeTransMod($lang,$_REQUEST['module'], $data);
126 // write merged file..
127 //$this->prefix = '_T["'.$lang .'"]=';
128 //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
133 * load strings that need translating..
136 function loadOriginalStrings($lang, $module)
138 // since this can handle errors better.!!?
139 $info = $this->moduleJavascriptFilesInfo($module);
141 $tfile = $info->translation_data;
143 if (empty($tfile) || !file_exists($tfile)) {
148 require_once 'Services/JSON.php';
149 $j = new Services_JSON();
151 return (array) $j->decode('{'. file_get_contents($tfile).'}');
159 * Load the user translated strings.
162 function loadTranslate($lang, $module)
165 $fn = $this->getTransFilename($lang,$module);
168 if (!file_exists($fn)) {
172 return (array) json_decode(file_get_contents($fn));
173 //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
177 function loadTranslateDB($lang, $module)
179 $d = DB_DataObject('translations');
180 $d->module = $module;
183 while ($d->fetch()) {
184 if (!isset($ret[$d->tfile])) {
185 $ret[$d->tfile] = array( $d->tkey => $d->tval );
188 $ret[$d->tfile][$d->tkey] = $d->tval;
193 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
195 $d = DB_DataObject('translations');
196 $d->module = $module;
200 if ($d->find(true)) {
212 function getTransFilename($lang, $module)
215 $ff = HTML_FlexyFramework::get();
216 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
217 if (!file_exists(dirname($fn))) {
219 /// create the direct
220 $oldumask = umask(0);
221 mkdir(dirname($fn), 0770, true);
224 if (!file_exists(dirname($fn))) {
225 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
234 function loadOld($lang,$data)
236 // need the old for hinting..
238 // this is ok - as it's inside this module.
239 $old = (array) json_decode(file_get_contents(dirname(__FILE__).'/data/oldeng.js'));
241 // contains key/value of data..
242 $prefix = '_T["'.$lang .'"]=';
243 $trans = (array) json_decode(substr(file_get_contents(
244 dirname(__FILE__).'/data/lang.' . $lang . '.js'
245 ), strlen($prefix), -1));
247 // echo '<PRE>';print_r($trans);
250 foreach($old as $k=>$v) {
251 if (isset($trans[$k]) && empty($hints[$v])) {
252 $hints[$v] = $trans[$k];
255 $translated_data = array();
256 foreach($data as $k=>$ar) {
257 foreach($ar as $tr=>$trv) {
258 $key = md5($k.'-'.$tr);
259 if (isset($hints[$tr])) {
260 $translated_data[$key] = $hints[$tr];
264 // $this->writeTransMod($lang, $module, $translated_data);
265 //echo '<PRE>';print_r($hints);
266 return $translated_data;
270 function writeTransMod($lang, $module, $data)
272 $fn = $this->getTransFilename($lang, $module);
273 file_put_contents($fn, json_encode($data));
274 $ff = HTML_FlexyFramework::get();
275 $base = $ff->rootDir.'/_translations_' ;
277 foreach(scandir($base) as $l) {
279 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
283 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
287 file_put_contents("$base/$module.js", $out);
289 //$this->writeTrans($lang);