Пример #1
0
 public function getDynamicTranslations()
 {
     $app = Application::getFacadeApplication();
     $fh = $app->make('helper/file');
     C5TLOptions::setTemporaryDirectory($fh->getTemporaryDirectory());
     $parser = new C5TLParserDynamic();
     return $parser->parseRunningConcrete5();
 }
Пример #2
0
 /**
  * Extracts translatable strings from PHP files with xgettext.
  *
  * @param string        $rootDirectory The base root directory
  * @param array[string] $phpFiles      The relative paths to the PHP files to be parsed
  *
  * @throws \Exception Throws an \Exception in case of problems
  *
  * @return \Gettext\Translations
  */
 protected static function parseDirectoryDo_xgettext($rootDirectory, $phpFiles)
 {
     $initialDirectory = @getcwd();
     if ($initialDirectory === false) {
         throw new \Exception('Unable to determine the current working directory');
     }
     if (@chdir($rootDirectory) === false) {
         throw new \Exception('Unable to switch to directory ' . $rootDirectory);
     }
     try {
         $tempDirectory = \C5TL\Options::getTemporaryDirectory();
         $tempFileList = @tempnam($tempDirectory, 'cil');
         if ($tempFileList === false) {
             throw new \Exception(t('Unable to create a temporary file'));
         }
         if (@file_put_contents($tempFileList, implode("\n", $phpFiles)) === false) {
             global $php_errormsg;
             if (isset($php_errormsg) && $php_errormsg) {
                 throw new \Exception("Error writing a temporary file: {$php_errormsg}");
             } else {
                 throw new \Exception('Error writing a temporary file');
             }
         }
         $tempFilePot = @tempnam($tempDirectory, 'cil');
         if ($tempFilePot === false) {
             throw new \Exception(t('Unable to create a temporary file'));
         }
         $line = 'xgettext';
         $line .= ' --default-domain=messages';
         // Domain
         $line .= ' --output=' . escapeshellarg(basename($tempFilePot));
         // Output .pot file name
         $line .= ' --output-dir=' . escapeshellarg(dirname($tempFilePot));
         // Output .pot folder name
         $line .= ' --language=PHP';
         // Source files are in php
         $line .= ' --from-code=UTF-8';
         // Source files are in utf-8
         $line .= ' --add-comments=i18n';
         // Place comment blocks preceding keyword lines in output file if they start with '// i18n: '
         $line .= ' --keyword';
         // Don't use default keywords
         $line .= ' --keyword=t:1';
         // Look for the first argument of the "t" function for extracting translatable text in singular form
         $line .= ' --keyword=t2:1,2';
         // Look for the first and second arguments of the "t2" function for extracting both the singular and plural forms
         $line .= ' --keyword=tc:1c,2';
         // Look for the first argument of the "tc" function for extracting translation context, and the second argument is the translatable text in singular form.
         $line .= ' --no-escape';
         // Do not use C escapes in output
         $line .= ' --add-location';
         // Generate '#: filename:line' lines
         $line .= ' --files-from=' . escapeshellarg($tempFileList);
         // Get list of input files from file
         $line .= ' 2>&1';
         $output = array();
         $rc = null;
         @exec($line, $output, $rc);
         @unlink($tempFileList);
         unset($tempFileList);
         if (!is_int($rc)) {
             $rc = -1;
         }
         if (!is_array($output)) {
             $output = array();
         }
         if ($rc !== 0) {
             throw new \Exception('xgettext failed: ' . implode("\n", $output));
         }
         $newTranslations = \Gettext\Translations::fromPoFile($tempFilePot);
         @unlink($tempFilePot);
         unset($tempFilePot);
     } catch (\Exception $x) {
         @chdir($initialDirectory);
         if (isset($tempFilePot) && @is_file($tempFilePot)) {
             @unlink($tempFilePot);
         }
         if (isset($tempFileList) && @is_file($tempFileList)) {
             @unlink($tempFileList);
         }
         throw $x;
     }
     return $newTranslations;
 }
Пример #3
0
 private function evaluate()
 {
     $tempDir = Options::getTemporaryDirectory();
     for ($i = 0;; ++$i) {
         $filename = rtrim($tempDir, '/' . DIRECTORY_SEPARATOR) . '/c5tltmp' . $i . '.php';
         if (!file_exists($filename)) {
             break;
         }
     }
     if (@file_put_contents($filename, $this->contents) === false) {
         throw new Exception('Failed to write a temporary file');
     }
     $errorReporting = @error_reporting();
     error_reporting($errorReporting & ~E_NOTICE);
     $exception = null;
     $autoloader = array($this, 'evaluateAutoloader');
     spl_autoload_register($autoloader, true, false);
     $prevErrorHandler = set_error_handler(array($this, evaluateHandleError));
     $this->lastEvaluateError = null;
     try {
         $code = (include $filename);
     } catch (\Exception $x) {
         $exception = $x;
     } catch (\Throwable $x) {
         $exception = new \Exception($x->getMessage());
     }
     @set_error_handler($prevErrorHandler);
     @unlink($filename);
     @spl_autoload_unregister($autoloader);
     @error_reporting($errorReporting);
     if ($exception !== null) {
         throw $exception;
     }
     if (!is_array($code) && $this->lastEvaluateError !== null) {
         throw new Exception('Failed to read configuration file: ' . $this->lastEvaluateError[1]);
     }
     $this->array = is_array($code) ? $code : array();
 }