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!'
68 echo '<PRE>';print_R($data);exit;
69 // covert data ready to send back..
72 foreach($data as $k=>$ar) {
73 foreach($ar as $tr=>$trv) {
74 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
75 $key = md5($k.'-'.$tr);
77 'id' => $lang.'/'.$key,
81 'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
99 $fm = HTML_FlexyFramework::get();
100 $enable = explode(',', $fm->enable);
101 if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
102 $this->jerr("NO MODULE / INVALID MODULE");
105 //id zh_HK/54e1d44609e3abed11f6e1eb6ae54988
107 list($lang,$id) = explode('/', $_REQUEST['id']);
110 $data = $this->loadTranslate($lang,$_REQUEST['module']);
112 $data[$id] = $_REQUEST['txt'];
115 $this->writeTransMod($lang,$_REQUEST['module'], $data);
116 // write merged file..
117 //$this->prefix = '_T["'.$lang .'"]=';
118 //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
123 * load strings that need translating..
126 function loadOriginalStrings($lang, $module)
128 // since this can handle errors better.!!?
129 $info = $this->moduleJavascriptFilesInfo($module);
131 $tfile =$info->basedir . '/'. $info->translation_data;
133 if (empty($tfile) || !file_exists($tfile)) {
138 require_once 'Services/JSON.php';
139 $j = new Services_JSON();
141 return (array) $j->decode('{'. file_get_contents($tfile).'}');
149 * Load the user translated strings.
152 function loadTranslate($lang, $module)
155 $fn = $this->getTransFilename($lang,$module);
158 if (!file_exists($fn)) {
162 return (array) json_decode(file_get_contents($fn));
163 //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
167 function loadTranslateDB($lang, $module)
169 $d = DB_DataObject('translations');
170 $d->module = $module;
173 while ($d->fetch()) {
174 if (!isset($ret[$d->tfile])) {
175 $ret[$d->tfile] = array( $d->tkey => $d->tval );
178 $ret[$d->tfile][$d->tkey] = $d->tval;
183 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
185 $d = DB_DataObject('translations');
186 $d->module = $module;
190 if ($d->find(true)) {
202 function getTransFilename($lang, $module)
205 $ff = HTML_FlexyFramework::get();
206 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
207 if (!file_exists(dirname($fn))) {
209 /// create the direct
210 $oldumask = umask(0);
211 mkdir(dirname($fn), 0770, true);
214 if (!file_exists(dirname($fn))) {
215 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
224 * Writes a file MODULE.js inside of _translations_
226 * this should contain all contents from all the language directroris for this
231 function writeTransMod($lang, $module, $data)
235 $fn = $this->getTransFilename($lang, $module);
236 file_put_contents($fn, json_encode($data));
237 $ff = HTML_FlexyFramework::get();
238 $base = $ff->rootDir.'/_translations_' ;
240 foreach(scandir($base) as $l) {
242 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
246 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
250 file_put_contents("$base/$module.js", $out);
252 //$this->writeTrans($lang);