Skip to content

Commit

Permalink
[TwigBundle] made trans and transchoice tags more flexible
Browse files Browse the repository at this point in the history
Both tags accept variables now:

    {% trans label %}

    {% transchoice %}
      {{ error }}
    {% endtranschoice %}

Optionally, the with keywords allows to pass the placeholder values:

    {% trans label with vars %}
  • Loading branch information
fabpot committed Sep 30, 2010
1 parent 4297609 commit eff1bdf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
48 changes: 38 additions & 10 deletions src/Symfony/Bundle/TwigBundle/Node/TransNode.php
Expand Up @@ -18,9 +18,9 @@
*/
class TransNode extends \Twig_Node
{
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, $lineno, $tag = null)
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $lineno, $tag = null)
{
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain), array(), $lineno, $tag);
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array(), $lineno, $tag);
}

/**
Expand All @@ -32,7 +32,12 @@ public function compile($compiler)
{
$compiler->addDebugInfo($this);

list($msg, $vars) = $this->compileString($this->body);
if ($this->isSimpleString($this->body)) {
list($msg, $vars) = $this->compileString($this->body);
} else {
$msg = $this->body;
$vars = $this->vars;
}

$method = null === $this->count ? 'trans' : 'transChoice';

Expand All @@ -52,13 +57,19 @@ public function compile($compiler)

$compiler->raw('array(');

foreach ($vars as $var) {
$compiler
->string('{{ '.$var['name'].' }}')
->raw(' => ')
->subcompile($var)
->raw(', ')
;
if (is_array($vars)) {
foreach ($vars as $var) {
$compiler
->string('{{ '.$var['name'].' }}')
->raw(' => ')
->subcompile($var)
->raw(', ')
;
}
} elseif (null !== $vars) {
$compiler->subcompile($vars);
} else {
$compiler->raw('array()');
}

$compiler
Expand Down Expand Up @@ -91,4 +102,21 @@ protected function compileString(\Twig_NodeInterface $body)

return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($msg), $node->getLine()))), $vars);
}

protected function isSimpleString(\Twig_NodeInterface $body)
{
foreach ($body as $i => $node) {
if (
$node instanceof \Twig_Node_Text
||
($node instanceof \Twig_Node_Print && $node->expr instanceof \Twig_Node_Expression_Name)
) {
continue;
}

return false;
}

return true;
}
}
Expand Up @@ -32,10 +32,20 @@ public function parse(\Twig_Token $token)
$lineno = $token->getLine();
$stream = $this->parser->getStream();

$vars = null;

$count = $this->parser->getExpressionParser()->parseExpression();

$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test('from')) {

if ($stream->test('with')) {
// {% transchoice count with vars %}
$stream->next();
$vars = $this->parser->getExpressionParser()->parseExpression();
}

if ($stream->test('from')) {
// {% transchoice count from "messages" %}
$stream->next();
$domain = $this->parser->getExpressionParser()->parseExpression();
}
Expand All @@ -46,9 +56,7 @@ public function parse(\Twig_Token $token)

$stream->expect(\Twig_Token::BLOCK_END_TYPE);

$this->checkTransString($body, $lineno);

return new TransNode($body, $domain, $count, $lineno, $this->getTag());
return new TransNode($body, $domain, $count, $vars, $lineno, $this->getTag());
}

public function decideTransChoiceFork($token)
Expand Down
27 changes: 9 additions & 18 deletions src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php
Expand Up @@ -32,14 +32,22 @@ public function parse(\Twig_Token $token)
$lineno = $token->getLine();
$stream = $this->parser->getStream();

$vars = null;
$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
$body = null;
if (!$stream->test('from')) {
// {% trans "message" %}
// {% trans message %}
$body = $this->parser->getExpressionParser()->parseExpression();
}

if ($stream->test('with')) {
// {% trans "message" with vars %}
$stream->next();
$vars = $this->parser->getExpressionParser()->parseExpression();
}

if ($stream->test('from')) {
// {% trans "message" from "messages" %}
$stream->next();
Expand All @@ -61,9 +69,7 @@ public function parse(\Twig_Token $token)

$stream->expect(\Twig_Token::BLOCK_END_TYPE);

$this->checkTransString($body, $lineno);

return new TransNode($body, $domain, null, $lineno, $this->getTag());
return new TransNode($body, $domain, null, $vars, $lineno, $this->getTag());
}

public function decideTransFork($token)
Expand All @@ -80,19 +86,4 @@ public function getTag()
{
return 'trans';
}

protected function checkTransString(\Twig_NodeInterface $body, $lineno)
{
foreach ($body as $i => $node) {
if (
$node instanceof \Twig_Node_Text
||
($node instanceof \Twig_Node_Print && $node->expr instanceof \Twig_Node_Expression_Name)
) {
continue;
}

throw new \Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
}
}
}

0 comments on commit eff1bdf

Please sign in to comment.