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->loadTranslateDB($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.
150 * {root}/_translation_/{lang}/{module}.js
156 function loadTranslate($lang, $module)
159 $fn = $this->getTransFilename($lang,$module);
162 if (!file_exists($fn)) {
166 return (array) json_decode(file_get_contents($fn));
167 //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
174 * @return key=>value list of translation_id=>tranlation.
179 function loadTranslateDB($lang, $module)
181 $d = DB_DataObject('translations');
182 $d->module = $module;
187 // since key includes file
188 $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
190 // no data is contained in the database, we should initialize it, if we can
191 $info = moduleJavascriptFilesInfo($module);
192 $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
193 if (!file_exists($fn)) {
196 $default = (array) json_decode(file_get_contents($fn));
197 foreach($default as $k=>$v) {
198 if (isset($ret[$k])) {
199 continue; // skip database already holds a version of this translation.
201 // is it relivant anymore..
213 function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
215 $d = DB_DataObject('translations');
216 $d->module = $module;
220 if ($d->find(true)) {
232 function getTransFilename($lang, $module)
235 $ff = HTML_FlexyFramework::get();
236 $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
237 if (!file_exists(dirname($fn))) {
239 /// create the direct
240 $oldumask = umask(0);
241 mkdir(dirname($fn), 0770, true);
244 if (!file_exists(dirname($fn))) {
245 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
254 * Writes a file MODULE.js inside of _translations_
256 * this should contain all contents from all the language directroris for this
261 function writeTransMod($lang, $module, $data)
265 $fn = $this->getTransFilename($lang, $module);
266 file_put_contents($fn, json_encode($data));
267 $ff = HTML_FlexyFramework::get();
268 $base = $ff->rootDir.'/_translations_' ;
270 foreach(scandir($base) as $l) {
272 if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
276 $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
280 file_put_contents("$base/$module.js", $out);
282 //$this->writeTrans($lang);