2010-09-27 08:45:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:58 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-03-23 14:23:52 +00:00
|
|
|
namespace Symfony\Bridge\Twig\TokenParser;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2011-03-23 14:23:52 +00:00
|
|
|
use Symfony\Bridge\Twig\Node\TransNode;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-09-27 08:45:58 +01:00
|
|
|
/**
|
2011-12-13 07:50:54 +00:00
|
|
|
* Token Parser for the 'trans' tag.
|
2010-09-27 08:45:58 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:58 +01:00
|
|
|
*/
|
|
|
|
class TransTokenParser extends \Twig_TokenParser
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Parses a token and returns a node.
|
|
|
|
*
|
|
|
|
* @param \Twig_Token $token A Twig_Token instance
|
|
|
|
*
|
|
|
|
* @return \Twig_NodeInterface A Twig_NodeInterface instance
|
|
|
|
*/
|
|
|
|
public function parse(\Twig_Token $token)
|
|
|
|
{
|
|
|
|
$lineno = $token->getLine();
|
|
|
|
$stream = $this->parser->getStream();
|
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
$vars = new \Twig_Node_Expression_Array(array(), $lineno);
|
2012-01-02 16:09:43 +00:00
|
|
|
$domain = null;
|
2011-07-26 00:27:09 +01:00
|
|
|
$locale = null;
|
2010-09-27 08:45:58 +01:00
|
|
|
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
|
2010-09-30 18:13:01 +01:00
|
|
|
if ($stream->test('with')) {
|
2011-04-21 08:04:16 +01:00
|
|
|
// {% trans with vars %}
|
2010-09-30 18:13:01 +01:00
|
|
|
$stream->next();
|
|
|
|
$vars = $this->parser->getExpressionParser()->parseExpression();
|
|
|
|
}
|
|
|
|
|
2010-09-27 08:45:58 +01:00
|
|
|
if ($stream->test('from')) {
|
2011-04-21 08:04:16 +01:00
|
|
|
// {% trans from "messages" %}
|
2010-09-27 08:45:58 +01:00
|
|
|
$stream->next();
|
|
|
|
$domain = $this->parser->getExpressionParser()->parseExpression();
|
2011-07-26 00:27:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stream->test('into')) {
|
|
|
|
// {% trans into "fr" %}
|
|
|
|
$stream->next();
|
|
|
|
$locale = $this->parser->getExpressionParser()->parseExpression();
|
2010-09-29 17:11:25 +01:00
|
|
|
} elseif (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
|
2011-04-21 08:03:30 +01:00
|
|
|
throw new \Twig_Error_Syntax('Unexpected token. Twig was looking for the "with" or "from" keyword.');
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
2010-10-02 08:39:48 +01:00
|
|
|
}
|
|
|
|
|
2011-04-21 08:04:16 +01:00
|
|
|
// {% trans %}message{% endtrans %}
|
|
|
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
|
|
|
$body = $this->parser->subparse(array($this, 'decideTransFork'), true);
|
2011-01-03 18:50:23 +00:00
|
|
|
|
|
|
|
if (!$body instanceof \Twig_Node_Text && !$body instanceof \Twig_Node_Expression) {
|
2012-05-09 14:13:29 +01:00
|
|
|
throw new \Twig_Error_Syntax('A message inside a trans tag must be a simple text');
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
|
|
|
|
2011-07-26 00:27:09 +01:00
|
|
|
return new TransNode($body, $domain, null, $vars, $locale, $lineno, $this->getTag());
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function decideTransFork($token)
|
|
|
|
{
|
|
|
|
return $token->test(array('endtrans'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the tag name associated with this token parser.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @return string The tag name
|
2010-09-27 08:45:58 +01:00
|
|
|
*/
|
|
|
|
public function getTag()
|
|
|
|
{
|
|
|
|
return 'trans';
|
|
|
|
}
|
|
|
|
}
|