Ejemplo n.º 1
0
 /**
  * @return \stdClass
  */
 private function getG11nInfo()
 {
     $info = new stdClass();
     $this->languages = Ecrg11nHelper::getLanguages();
     $baseLink = '';
     $info->scope = $this->project->scope ? $this->project->scope : 'admin';
     $info->id = $this->project->dbId;
     $info->extension = $this->project->comName;
     switch ($this->project->type) {
         case 'component':
             $comName = $this->project->comName;
             break;
         case 'plugin':
             $comName = 'plg_content_' . $this->project->comName;
             break;
         case 'template':
             $comName = 'tpl_' . $this->project->comName;
             break;
         default:
             echo 'undefined type: ' . $this->project->type;
             $comName = $this->project->comName;
             break;
     }
     //switch
     $info->exists = g11nExtensionHelper::isExtension($comName, $info->scope);
     $info->templateLink = $info->exists ? $baseLink . '&task=g11n.createTemplate&extension=' . $this->project->comName : '';
     $cachedFiles = Ecrg11nHelper::getCachedFiles();
     $this->scopes = array('admin' => JPATH_ADMINISTRATOR, 'site' => JPATH_SITE);
     $baseLink = 'index.php?option=com_g11n';
     $info->editLink = $baseLink . '&task=g11n.edit';
     $info->templateCommands = array();
     $info->updateLinks = array();
     $info->cacheLinks = array();
     $types = array('');
     $options = new JRegistry($this->project->buildOpts);
     if ('ON' == $options->get('lng_separate_javascript')) {
         $types[] = '.js';
     }
     $types[] = '.config';
     $extensionName = $this->project->comName;
     if (strpos($extensionName, '.')) {
         $extensionName = substr($extensionName, 0, strpos($extensionName, '.'));
     }
     foreach ($this->scopes as $scope => $path) {
         foreach ($types as $type) {
             if ('.config' == $type && 'admin' != $scope) {
                 continue;
             }
             $scopeType = $scope . $type;
             if (!isset($this->languages[$scopeType])) {
                 $this->languages[$scopeType] = $this->languages[$scope];
             }
             try {
                 $info->templateExists[$scopeType] = g11nStorage::templateExists($comName . $type, $scope);
             } catch (Exception $e) {
                 $info->templateCommands[$scopeType] = $e->getMessage();
                 $info->templateLink = '';
             }
             //try
             try {
                 $info->templateStatus[$scopeType] = g11nStorage::templateExists($comName . $type, $scope);
             } catch (Exception $e) {
                 $info->templateStatus[$scopeType] = $e->getMessage();
                 echo '';
             }
             //try
             foreach ($this->languages[$scopeType] as $lang) {
                 $exists = g11nExtensionHelper::findLanguageFile($lang['tag'], $comName . $type, $scope);
                 $info->fileStatus[$scopeType][$lang['tag']] = $exists ? true : false;
                 //                     g11nExtensionHelper::findLanguageFile($lang['tag']
                 //                    , $item->extension, $scope);
                 $link = $baseLink . '&task=utility.updateLanguage';
                 $link .= '&extension=' . $info->extension . '&scope=' . $scope;
                 $link .= '&langTag=' . $lang['tag'];
                 $info->updateLinks[$scopeType][$lang['tag']] = $link;
                 if (!array_key_exists($extensionName, $cachedFiles) || !array_key_exists($scope, $cachedFiles[$extensionName])) {
                     $info->cacheStatus[$scopeType][$lang['tag']] = false;
                     continue;
                 }
                 $info->cacheStatus[$scopeType][$lang['tag']] = false;
                 $fName = $lang['tag'] . '.' . $this->project->comName;
                 foreach ($cachedFiles[$extensionName][$scope] as $file) {
                     if (strpos($file, $fName) === 0) {
                         $info->cacheStatus[$scope][$lang['tag']] = true;
                     }
                 }
                 //foreach
             }
             //foreach
         }
         //foreach
     }
     return $info;
 }
Ejemplo n.º 2
0
 /**
  * Create a language template file.
  *
  * @static
  *
  * @param string $extension Extension name
  * @param string $scope Scope mus be "admin" or "site"
  *
  * @param string $version Version string
  * @param string $fileName Process only a single file
  *
  * @return bool
  * @throws Exception
  */
 public static function createTemplate($extension, $scope, $version = '1.0', $fileName = null)
 {
     if ($scope != 'admin' && $scope != 'site') {
         throw new Exception('Scope must be "admin" or "site"');
     }
     $base = g11nExtensionHelper::getScopePath($scope);
     $templatePath = g11nStorage::getTemplatePath($extension, $scope);
     $extensionDir = g11nExtensionHelper::getExtensionPath($extension);
     if (!JFolder::exists($base . DS . $extensionDir)) {
         throw new Exception('Invalid extension');
     }
     $dirName = dirname($templatePath);
     if (!JFolder::exists($dirName) && !JFolder::create($dirName)) {
         throw new Exception(jgettext('Can not create the language template folder'));
     }
     $headerData = '';
     $headerData .= ' --copyright-holder="Nikolai Plath - elkuku"';
     $headerData .= ' --package-name="' . $extension . '"';
     $headerData .= ' --package-version="' . $version . '"';
     $headerData .= ' --msgid-bugs-address="*****@*****.**"';
     $comments = ' --add-comments=TRANSLATORS:';
     $keywords = ' -k --keyword=jgettext --keyword=jngettext:1,2';
     $forcePo = ' --force-po --no-wrap';
     $subType = strpos($extension, '.') ? substr($extension, strpos($extension, '.') + 1) : '';
     $buildOpts = '';
     $excludes = array('/editarea_0_8_1_1/', '/highcharts-2.0.5/', '/php2js.js', 'krumo_0_2');
     $title = $extension . ' - ' . $scope;
     $search = 'php';
     $path = $base . DS . $extensionDir;
     switch ($subType) {
         case '':
             $search = 'php';
             $title .= ' - The main language file.';
             break;
         case 'js':
             $search = 'js';
             $buildOpts .= ' -L python';
             $title .= ' - The javascript language file.';
             $parts = g11nExtensionHelper::split($extension);
             $p = JPATH_ROOT . '/media/' . $parts[0] . '/' . $scope;
             if (JFolder::exists($p)) {
                 //-- Change the path to the media folder
                 $path = $p;
             }
             $excludes[] = '/templates/';
             $excludes[] = '.min.';
             break;
         case 'config':
             $search = 'xml';
             $buildOpts .= ' -L Glade';
             $keywords = ' -k --keyword=description --keyword=label';
             $excludes[] = '/templates/';
             $excludes[] = '/scripts/';
             $excludes[] = '/data/';
             $title .= ' - The configuration language file.';
             break;
         default:
             break;
     }
     $files = JFolder::files($path, '.' . $search . '$', true, true);
     if (!$files) {
         throw new Exception(jgettext('No files found'));
     }
     $cleanFiles = array();
     foreach ($files as $file) {
         $found = false;
         foreach ($excludes as $exclude) {
             if (strpos($file, $exclude)) {
                 $found = true;
             }
         }
         if (!$found) {
             $cleanFiles[] = $file;
         }
     }
     if ('config' == $subType) {
         defined('NL') || define('NL', "\n");
         $parser = g11n::getParser('code', 'xml');
         $potParser = g11n::getParser('language', 'pot');
         $options = new JObject();
         $outFile = new g11nFileInfo();
         foreach ($cleanFiles as $fileName) {
             $fileInfo = $parser->parse($fileName);
             if (!count($fileInfo->strings)) {
                 continue;
             }
             $relPath = str_replace(JPATH_ROOT . DS, '', $fileName);
             foreach ($fileInfo->strings as $key => $strings) {
                 foreach ($strings as $string) {
                     if (array_key_exists($string, $outFile->strings)) {
                         if (strpos($outFile->strings[$string]->info, $relPath . ':' . $key) !== false) {
                             continue;
                         }
                         $outFile->strings[$string]->info .= '#: ' . $relPath . ':' . $key . NL;
                         continue;
                     }
                     $t = new g11nTransInfo();
                     $t->info .= '#: ' . $relPath . ':' . $key . NL;
                     $outFile->strings[$string] = $t;
                 }
                 //foreach
             }
             //foreach
         }
         //foreach
         $buffer = $potParser->generate($outFile, $options);
         if (!JFile::write($templatePath, $buffer)) {
             throw new Exception('Unable to write the output file');
         }
     } else {
         $fileList = implode("\n", $cleanFiles);
         $command = $keywords . $buildOpts . ' -o ' . $templatePath . $forcePo . $comments . $headerData;
         echo '<h3>FILELIST | xgettext ' . $command . ' -f - 2>&1</h3>';
         ob_start();
         system('echo "' . $fileList . '" | xgettext ' . $command . ' -f - 2>&1');
         $result = ob_get_clean();
         echo '<pre>' . $result . '</pre>';
     }
     if (!JFile::exists($templatePath)) {
         throw new Exception('Could not create the template');
     }
     //-- Manually strip the JROOT path
     $contents = JFile::read($templatePath);
     $contents = str_replace(JPATH_ROOT . DS, '', $contents);
     //-- Replace the title
     $contents = str_replace('# SOME DESCRIPTIVE TITLE.', '# ' . $title, $contents);
     if (!JFile::write($templatePath, $contents)) {
         throw new Exception(sprintf('Unable to write to path: %s', $templatePath));
     }
     return true;
 }