2011-08-29 18:22:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Translation;
|
|
|
|
|
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
use Symfony\Component\Translation\MessageCatalogue;
|
|
|
|
use Symfony\Component\Translation\Extractor\ExtractorInterface;
|
|
|
|
|
|
|
|
/**
|
2013-12-27 15:08:19 +00:00
|
|
|
* PhpExtractor extracts translation messages from a PHP template.
|
2011-10-29 11:05:45 +01:00
|
|
|
*
|
2011-08-29 18:22:52 +01:00
|
|
|
* @author Michel Salib <michelsalib@hotmail.com>
|
|
|
|
*/
|
|
|
|
class PhpExtractor implements ExtractorInterface
|
|
|
|
{
|
|
|
|
const MESSAGE_TOKEN = 300;
|
2011-10-29 11:05:45 +01:00
|
|
|
|
2011-08-29 18:22:52 +01:00
|
|
|
/**
|
|
|
|
* Prefix for new found message.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $prefix = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sequence that captures translation messages.
|
2011-10-29 11:05:45 +01:00
|
|
|
*
|
|
|
|
* @var array
|
2011-08-29 18:22:52 +01:00
|
|
|
*/
|
|
|
|
protected $sequences = array(
|
|
|
|
array(
|
|
|
|
'->',
|
|
|
|
'trans',
|
|
|
|
'(',
|
|
|
|
self::MESSAGE_TOKEN,
|
2014-02-04 08:26:21 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'->',
|
|
|
|
'transChoice',
|
|
|
|
'(',
|
|
|
|
self::MESSAGE_TOKEN,
|
2011-08-29 18:22:52 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-08-29 18:22:52 +01:00
|
|
|
*/
|
|
|
|
public function extract($directory, MessageCatalogue $catalog)
|
|
|
|
{
|
|
|
|
// load any existing translation files
|
|
|
|
$finder = new Finder();
|
|
|
|
$files = $finder->files()->name('*.php')->in($directory);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog);
|
|
|
|
}
|
|
|
|
}
|
2011-10-29 11:05:45 +01:00
|
|
|
|
2011-08-29 18:22:52 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-08-29 18:22:52 +01:00
|
|
|
*/
|
|
|
|
public function setPrefix($prefix)
|
|
|
|
{
|
|
|
|
$this->prefix = $prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes a token.
|
2011-10-29 11:05:45 +01:00
|
|
|
*
|
2014-02-04 08:26:21 +00:00
|
|
|
* @param mixed $token
|
2011-10-29 11:05:45 +01:00
|
|
|
* @return string
|
2011-08-29 18:22:52 +01:00
|
|
|
*/
|
|
|
|
protected function normalizeToken($token)
|
|
|
|
{
|
|
|
|
if (is_array($token)) {
|
|
|
|
return $token[1];
|
|
|
|
}
|
2011-10-29 11:05:45 +01:00
|
|
|
|
2011-08-29 18:22:52 +01:00
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2014-02-04 08:26:21 +00:00
|
|
|
/**
|
|
|
|
* Seeks to a non-whitespace token
|
|
|
|
*
|
|
|
|
* @param \ArrayIterator $tokenIterator
|
|
|
|
*/
|
|
|
|
protected function seekToNextRelaventToken($tokenIterator)
|
|
|
|
{
|
|
|
|
for ( ; $tokenIterator->valid(); $tokenIterator->next()) {
|
|
|
|
$t = $tokenIterator->current();
|
|
|
|
if (!is_array($t) || ($t[0] !== T_WHITESPACE)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts the message from the iterator while the tokens
|
|
|
|
* match allowed message tokens
|
|
|
|
*
|
|
|
|
* @param \ArrayIterator $tokenIterator
|
|
|
|
*/
|
|
|
|
protected function getMessage($tokenIterator)
|
|
|
|
{
|
|
|
|
$message = '';
|
|
|
|
$docToken = '';
|
|
|
|
|
|
|
|
for ( ; $tokenIterator->valid(); $tokenIterator->next()) {
|
|
|
|
$t = $tokenIterator->current();
|
|
|
|
if (!is_array($t)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($t[0]) {
|
|
|
|
case T_START_HEREDOC:
|
|
|
|
$docToken = $t[1];
|
|
|
|
break;
|
|
|
|
case T_ENCAPSED_AND_WHITESPACE:
|
|
|
|
case T_CONSTANT_ENCAPSED_STRING:
|
|
|
|
$message .= $t[1];
|
|
|
|
break;
|
|
|
|
case T_END_HEREDOC:
|
|
|
|
return PhpStringTokenParser::parseDocString($docToken, $message);
|
|
|
|
default:
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($message) {
|
|
|
|
$message = PhpStringTokenParser::parse($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2011-08-29 18:22:52 +01:00
|
|
|
/**
|
2013-12-27 15:08:19 +00:00
|
|
|
* Extracts trans message from PHP tokens.
|
2011-10-29 11:05:45 +01:00
|
|
|
*
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param array $tokens
|
2011-10-29 11:05:45 +01:00
|
|
|
* @param MessageCatalogue $catalog
|
2011-08-29 18:22:52 +01:00
|
|
|
*/
|
|
|
|
protected function parseTokens($tokens, MessageCatalogue $catalog)
|
|
|
|
{
|
2014-02-04 08:26:21 +00:00
|
|
|
$tokenIterator = new \ArrayIterator($tokens);
|
|
|
|
|
|
|
|
for ($key = 0; $key < $tokenIterator->count(); $key++) {
|
2011-08-29 18:22:52 +01:00
|
|
|
foreach ($this->sequences as $sequence) {
|
|
|
|
$message = '';
|
2014-02-04 08:26:21 +00:00
|
|
|
$tokenIterator->seek($key);
|
2011-08-29 18:22:52 +01:00
|
|
|
|
2014-02-04 08:26:21 +00:00
|
|
|
foreach ($sequence as $item) {
|
|
|
|
$this->seekToNextRelaventToken($tokenIterator);
|
|
|
|
|
|
|
|
if ($this->normalizeToken($tokenIterator->current()) == $item) {
|
|
|
|
$tokenIterator->next();
|
2011-08-29 18:22:52 +01:00
|
|
|
continue;
|
|
|
|
} elseif (self::MESSAGE_TOKEN == $item) {
|
2014-02-04 08:26:21 +00:00
|
|
|
$message = $this->getMessage($tokenIterator);
|
|
|
|
break;
|
2011-08-29 18:22:52 +01:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($message) {
|
|
|
|
$catalog->set($message, $this->prefix.$message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|