From bd9cdadb71b9cb46e7c8c629739997d2a0c1388e Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 15 Jul 2014 16:06:07 -0300 Subject: [i18n] Added administration interface for the i18n system --- modules/admin.inc.php | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 modules/admin.inc.php (limited to 'modules/admin.inc.php') diff --git a/modules/admin.inc.php b/modules/admin.inc.php new file mode 100644 index 00000000..b74b5ef5 --- /dev/null +++ b/modules/admin.inc.php @@ -0,0 +1,160 @@ +template = Request::any('template'); + } + + } + + /** + * Implementation of the abstract doRender function + * + * Fetch the list of news from the database and paginate it. + * + */ + protected function doRender() + { + if(!$this->template || !$this->templateAnalysis($this->template)){ + $this->initTable(); + Render::addTemplate('administration/_page', array( + 'token' => Session::get('token'), + 'adminMessage' => $this->message, + 'table' => $this->table + )); + }else{ + Render::addTemplate('administration/template', array( + 'template' => $this->template, + 'tags' => $this->tags + )); + } + + + } + + private function initTable(){ + $this->listTemplates(); + $de = $this->listJson('de/'); + $en = $this->listJson('en/'); + $pt = $this->listJson('pt/'); + + foreach($this->files as $key => $value){ + + $this->table[] = array( + 'template' => $value, + 'link' => $key, + 'de' => $this->checkJson($de[$key],'de'), + 'en' => $this->checkJson($en[$key],'en'), + 'pt' => $this->checkJson($pt[$key],'pt') + ); + } + + } + + private function listTemplates(){ + $this->files = array(); + $dir = 'templates/'; + $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); + foreach($objects as $name => $object){ + if(array_pop(explode('.',$name)) === 'html'){ + $key = str_replace($dir, '', $name); + $key = str_replace('.html', '', $key); + $this->files[$key] = $name; + } + } + } + + private function listJson($lang){ + $json = array(); + $dir = 'lang/' . $lang; + $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); + foreach($objects as $name => $object){ + if(array_pop(explode('.',$name)) === 'json'){ + $key = str_replace($dir, '', $name); + $key = str_replace('.json', '', $key); + $json[$key] = $key; + } + } + return $json; + } + + private function checkJson($path,$lang){ + if($path){ + $htmlTemplate = file_get_contents('templates/' . $path . '.html'); + $json = Dictionary::getArrayTemplate($path,$lang); + $htmlCount = substr_count($htmlTemplate, 'lang_'); + $matchCount = 0; + + foreach($json as $key => $value){ + if($key != 'lang'){ + $key = $key . '}}'; + $matchCount += substr_count($htmlTemplate, $key); + } + } + + $diff = $htmlCount - $matchCount; + + //allright + if($diff == 0) return "OK"; + if($diff > 0) return $diff . " JSON tag(s) are missing"; + if($diff < 0) return ($diff * -1) . " extra JSON tag(s)"; + }else{ + return "JSON file is missing"; + } + + } + + private function templateAnalysis($path){ + if(!file_exists('templates/' . $path . '.html')){ + Message::addError('invalid-template'); + return false; + } + $htmlTemplate = file_get_contents('templates/' . $path . '.html'); + preg_match_all('/{{lang_(.*?)}}/s', $htmlTemplate, $matches); + + $tags = $matches[1]; + + foreach($tags as $tag){ + $this->tags[] = array( + 'tag' => 'lang_' . $tag, + 'de' => $this->checkJsonTag($path,$tag,'de/'), + 'en' => $this->checkJsonTag($path,$tag,'en/'), + 'pt' => $this->checkJsonTag($path,$tag,'pt/') + ); + } + + return true; + } + + private function checkJsonTag($path,$tag,$lang){ + if($json = Dictionary::getArrayTemplate($path,$lang)){ + return $json['lang_' . $tag]; + } + return ''; + } +} -- cgit v1.2.3-55-g7522