Skip to content

Commit

Permalink
[TwigBridge] Made the locale configurable for the trans and transchoi…
Browse files Browse the repository at this point in the history
…ce tags
  • Loading branch information
stof committed Jul 25, 2011
1 parent 3ea31a0 commit 85c0087
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Symfony/Bridge/Twig/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, \Twig_Node_Expression $vars = null, $lineno = 0, $tag = null)
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
{
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array(), $lineno, $tag);
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
}

/**
Expand Down Expand Up @@ -71,8 +71,14 @@ public function compile(\Twig_Compiler $compiler)
$compiler
->raw(', ')
->subcompile($this->getNode('domain'))
->raw(");\n")
;
if (null !== $this->getNode('locale')) {
$compiler
->raw(', ')
->subcompile($this->getNode('locale'))
;
}
$compiler->raw(");\n");
}

protected function compileDefaults(\Twig_Compiler $compiler, \Twig_Node_Expression_Array $defaults)
Expand Down
Expand Up @@ -37,6 +37,7 @@ public function parse(\Twig_Token $token)
$count = $this->parser->getExpressionParser()->parseExpression();

$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
$locale = null;

if ($stream->test('with')) {
// {% transchoice count with vars %}
Expand All @@ -50,6 +51,12 @@ public function parse(\Twig_Token $token)
$domain = $this->parser->getExpressionParser()->parseExpression();
}

if ($stream->test('into')) {
// {% transchoice count into "fr" %}
$stream->next();
$locale = $this->parser->getExpressionParser()->parseExpression();
}

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

$body = $this->parser->subparse(array($this, 'decideTransChoiceFork'), true);
Expand All @@ -60,7 +67,7 @@ public function parse(\Twig_Token $token)

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

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

public function decideTransChoiceFork($token)
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Bridge/Twig/TokenParser/TransTokenParser.php
Expand Up @@ -34,6 +34,7 @@ public function parse(\Twig_Token $token)

$vars = new \Twig_Node_Expression_Array(array(), $lineno);
$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
$locale = null;
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
if ($stream->test('with')) {
// {% trans with vars %}
Expand All @@ -45,6 +46,12 @@ public function parse(\Twig_Token $token)
// {% trans from "messages" %}
$stream->next();
$domain = $this->parser->getExpressionParser()->parseExpression();
}

if ($stream->test('into')) {
// {% trans into "fr" %}
$stream->next();
$locale = $this->parser->getExpressionParser()->parseExpression();
} elseif (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
throw new \Twig_Error_Syntax('Unexpected token. Twig was looking for the "with" or "from" keyword.');
}
Expand All @@ -60,7 +67,7 @@ public function parse(\Twig_Token $token)

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

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

public function decideTransFork($token)
Expand Down
Expand Up @@ -63,6 +63,8 @@ public function getTransTests()
array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),

array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),

// transchoice
array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
'There is no apples', array('count' => 0)),
Expand All @@ -72,6 +74,8 @@ public function getTransTests()
'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
'There is 5 apples (Symfony2)', array('count' => 5)),
array('{% transchoice count into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
'There is no apples', array('count' => 0)),

// trans filter
array('{{ "Hello"|trans }}', 'Hello'),
Expand Down

0 comments on commit 85c0087

Please sign in to comment.