/** * {@inheritdoc} */ public static function fromString($string, Translations $translations, array $options = []) { $cachePath = empty($options['cachePath']) ? sys_get_temp_dir() : $options['cachePath']; $bladeCompiler = new BladeCompiler(new Filesystem(), $cachePath); $string = $bladeCompiler->compileString($string); PhpCode::fromString($string, $translations, $options); }
/** * {@inheritDoc} */ public static function fromString($string, Translations $translations = null, $file = '') { self::addExtension('Twig_Extensions_Extension_I18n'); $string = self::$twig->compileSource($string); // add default global php gettext functions PhpCode::$functions['gettext'] = '__'; PhpCode::$functions['ngettext'] = '__'; PhpCode::$functions['_'] = '__'; return PhpCode::fromString($string, $translations, $file); }
protected static function parseFile($file, $sourceDir = null) { if ($sourceDir) { if (defined('REDCAT_PUBLIC')) { $cwd = REDCAT_PUBLIC; } else { $cwd = getcwd(); } chdir($sourceDir); //$file = substr($file,strlen($sourceDir)); } $msg = ''; $translations = PhpCode::fromFile($file); foreach ($translations as $translation) { $tr = []; if ($translation->hasComments()) { foreach ($translation->getComments() as $comment) { $tr[] = '# ' . $comment; } } if ($translation->hasReferences()) { foreach ($translation->getReferences() as $reference) { $tr[] = '#: ' . $reference[0] . (!is_null($reference[1]) ? ':' . $reference[1] : null); } } if ($translation->hasContext()) { $tr[] = 'msgctxt ' . self::quote($translation->getContext()); } $msgid = self::multilineQuote($translation->getOriginal()); if (count($msgid) === 1) { $tr[] = 'msgid ' . $msgid[0]; } else { $tr[] = 'msgid ""'; $tr = array_merge($tr, $msgid); } if ($translation->hasPlural()) { $tr[] = 'msgid_plural ' . self::quote($translation->getPlural()); $tr[] = 'msgstr[0] ' . self::quote($translation->getTranslation()); foreach ($translation->getPluralTranslation() as $k => $v) { $tr[] = 'msgstr[' . ($k + 1) . '] ' . self::quote($v); } } else { $tr[] = 'msgstr ' . self::quote($translation->getTranslation()); } $tr[] = "\n"; $msg .= implode("\n", $tr); } if (isset($cwd)) { chdir($cwd); } return $msg; }
private static function scan() { Extractors\PhpCode::$functions = ['__' => '__', '_' => '__']; $entries = new Translations(); foreach (self::$config['directories'] as $dir) { if (!is_dir($dir)) { throw new Exception(__('Folder %s not exists. Gettext scan aborted.', $dir)); } foreach (self::scanDir($dir) as $file) { if (strstr($file, '.blade.php')) { $entries->mergeWith(Extractors\Blade::fromFile($file)); } elseif (strstr($file, '.php')) { $entries->mergeWith(Extractors\PhpCode::fromFile($file)); } } } return $entries; }
/** * {@inheritdoc} */ public static function fromString($string, Translations $translations = null, $file = '') { $bladeCompiler = new BladeCompiler(new Filesystem(), null); $string = $bladeCompiler->compileString($string); return PhpCode::fromString($string, $translations, $file); }
/** * {@inheritdoc} */ public function getFunctions() { $count = count($this->tokens); $bufferFunctions = array(); /* @var ParsedFunction[] $bufferFunctions */ $functions = array(); /* @var ParsedFunction[] $functions */ for ($k = 0; $k < $count; ++$k) { $value = $this->tokens[$k]; if (is_string($value)) { if (isset($bufferFunctions[0])) { switch ($value) { case ',': $bufferFunctions[0]->nextArgument(); break; case ')': $functions[] = array_shift($bufferFunctions)->close(); break; case '.': break; default: $bufferFunctions[0]->stopArgument(); break; } } continue; } switch ($value[0]) { case T_CONSTANT_ENCAPSED_STRING: //add an argument to the current function if (isset($bufferFunctions[0])) { $bufferFunctions[0]->addArgumentChunk(PhpCode::convertString($value[1])); } break; case T_STRING: if (isset($bufferFunctions[0])) { $bufferFunctions[0]->stopArgument(); } //new function found for ($j = $k + 1; $j < $count; ++$j) { $nextToken = $this->tokens[$j]; if (is_array($nextToken) && $nextToken[0] === T_COMMENT) { continue; } if ($nextToken === '(') { $newFunction = new ParsedFunction($value[1], $value[2]); if ($k > 0 && is_array($this->tokens[$k - 1]) && $this->tokens[$k - 1][0] === T_COMMENT) { $comment = $this->parsePhpComment($this->tokens[$k - 1][1]); if ($comment !== null) { $newFunction->addComment($comment); } } array_unshift($bufferFunctions, $newFunction); $k = $j; } break; } break; case T_COMMENT: if (isset($bufferFunctions[0])) { $comment = $this->parsePhpComment($value[1]); if ($comment !== null) { $bufferFunctions[0]->addComment($comment); } } break; default: if (isset($bufferFunctions[0])) { $bufferFunctions[0]->stopArgument(); } break; } } return $functions; }
/** * {@inheritdoc} */ public static function fromString($string, Translations $translations, array $options = []) { $options += static::$options; $twig = $options['twig'] ?: self::createTwig(); PhpCode::fromString($twig->compileSource($string), $translations, $options); }