Skip to content

Commit

Permalink
[TwigBundle] added helpers for translations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 27, 2010
1 parent a753790 commit d6f55c3
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Extension/Helpers.php
Expand Up @@ -4,6 +4,8 @@

use Symfony\Component\Templating\Engine;
use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
use Symfony\Bundle\TwigBundle\TokenParser\TransTokenParser;
use Symfony\Bundle\TwigBundle\TokenParser\TransChoiceTokenParser;

/*
* This file is part of the Symfony package.
Expand Down Expand Up @@ -51,6 +53,14 @@ public function getTokenParsers()

// {% flash 'notice' %}
new HelperTokenParser('flash', '<name>', 'session', 'flash'),

// {% trans "Symfony is great!" %}
new TransTokenParser(),

// {% transchoice count %}
// {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
// {% endtranschoice %}
new TransChoiceTokenParser(),
);
}

Expand Down
94 changes: 94 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Node/TransNode.php
@@ -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);
}
}
@@ -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 src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php
@@ -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);
}
}
}

0 comments on commit d6f55c3

Please sign in to comment.