-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TwigBundle] added helpers for translations
- Loading branch information
Showing
4 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\TwigBundle\Node; | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class TransNode extends \Twig_Node | ||
{ | ||
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, $lineno, $tag = null) | ||
{ | ||
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain), array(), $lineno, $tag); | ||
} | ||
|
||
/** | ||
* Compiles the node to PHP. | ||
* | ||
* @param \Twig_Compiler A Twig_Compiler instance | ||
*/ | ||
public function compile($compiler) | ||
{ | ||
$compiler->addDebugInfo($this); | ||
|
||
list($msg, $vars) = $this->compileString($this->body); | ||
|
||
$method = null === $this->count ? 'trans' : 'transChoice'; | ||
|
||
$compiler | ||
->write('echo $context[\'_view\'][\'translator\']->'.$method.'(') | ||
->subcompile($msg) | ||
; | ||
|
||
$compiler->raw(', '); | ||
|
||
if (null !== $this->count) { | ||
$compiler | ||
->subcompile($this->count) | ||
->raw(', ') | ||
; | ||
} | ||
|
||
$compiler->raw('array('); | ||
|
||
foreach ($vars as $var) { | ||
$compiler | ||
->string('{{ '.$var['name'].' }}') | ||
->raw(' => ') | ||
->subcompile($var) | ||
->raw(', ') | ||
; | ||
} | ||
|
||
$compiler | ||
->raw("), ") | ||
->subcompile($this->domain) | ||
->raw(");\n") | ||
; | ||
} | ||
|
||
protected function compileString(\Twig_NodeInterface $body) | ||
{ | ||
if ($body instanceof \Twig_Node_Expression_Name || $body instanceof \Twig_Node_Expression_Constant) { | ||
return array($body, array()); | ||
} | ||
|
||
$msg = ''; | ||
$vars = array(); | ||
foreach ($body as $node) { | ||
if ($node instanceof \Twig_Node_Print) { | ||
$n = $node->expr; | ||
while ($n instanceof \Twig_Node_Expression_Filter) { | ||
$n = $n->node; | ||
} | ||
$msg .= sprintf('{{ %s }}', $n['name']); | ||
$vars[] = new \Twig_Node_Expression_Name($n['name'], $n->getLine()); | ||
} else { | ||
$msg .= $node['data']; | ||
} | ||
} | ||
|
||
return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($msg), $node->getLine()))), $vars); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Symfony/Bundle/TwigBundle/TokenParser/TransChoiceTokenParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\TwigBundle\TokenParser; | ||
|
||
use Symfony\Bundle\TwigBundle\Node\TransNode; | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class TransChoiceTokenParser extends TransTokenParser | ||
{ | ||
/** | ||
* 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(); | ||
|
||
$count = $this->parser->getExpressionParser()->parseExpression(); | ||
|
||
$domain = new \Twig_Node_Expression_Constant('messages', $lineno); | ||
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test('from')) { | ||
$stream->next(); | ||
$domain = $this->parser->getExpressionParser()->parseExpression(); | ||
} | ||
|
||
$stream->expect(\Twig_Token::BLOCK_END_TYPE); | ||
|
||
$body = $this->parser->subparse(array($this, 'decideTransChoiceFork'), true); | ||
|
||
$stream->expect(\Twig_Token::BLOCK_END_TYPE); | ||
|
||
$this->checkTransString($body, $lineno); | ||
|
||
return new TransNode($body, $domain, $count, $lineno, $this->getTag()); | ||
} | ||
|
||
public function decideTransChoiceFork($token) | ||
{ | ||
return $token->test(array('endtranschoice')); | ||
} | ||
|
||
/** | ||
* Gets the tag name associated with this token parser. | ||
* | ||
* @param string The tag name | ||
*/ | ||
public function getTag() | ||
{ | ||
return 'transchoice'; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\TwigBundle\TokenParser; | ||
|
||
use Symfony\Bundle\TwigBundle\Node\TransNode; | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
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(); | ||
|
||
$domain = new \Twig_Node_Expression_Constant('messages', $lineno); | ||
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { | ||
if (!$stream->test('from')) { | ||
$body = $this->parser->getExpressionParser()->parseExpression(); | ||
} | ||
|
||
if ($stream->test('from')) { | ||
$stream->next(); | ||
$domain = $this->parser->getExpressionParser()->parseExpression(); | ||
} | ||
} else { | ||
$stream->expect(\Twig_Token::BLOCK_END_TYPE); | ||
$body = $this->parser->subparse(array($this, 'decideTransFork')); | ||
} | ||
|
||
$stream->expect(\Twig_Token::BLOCK_END_TYPE); | ||
|
||
$this->checkTransString($body, $lineno); | ||
|
||
return new TransNode($body, $domain, null, $lineno, $this->getTag()); | ||
} | ||
|
||
public function decideTransFork($token) | ||
{ | ||
return $token->test(array('endtrans')); | ||
} | ||
|
||
/** | ||
* Gets the tag name associated with this token parser. | ||
* | ||
* @param string The tag name | ||
*/ | ||
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); | ||
} | ||
} | ||
} |