*/
class Twig_Node_Trans extends Twig_Node
{
- public function __construct(Twig_Node_Expression $count = null, Twig_NodeInterface $body, Twig_NodeInterface $plural = null, $lineno, $tag = null)
+ public function __construct(Twig_NodeInterface $body, Twig_NodeInterface $plural = null, Twig_Node_Expression $count = null, $lineno, $tag = null)
{
parent::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural), array(), $lineno, $tag);
}
foreach ($body as $i => $node) {
if ($node instanceof Twig_Node_Text) {
$msg .= $node['data'];
- } elseif ($node instanceof Twig_Node_Print && $node->expr instanceof Twig_Node_Expression_Name) {
- $msg .= sprintf('%%%s%%', $node->expr['name']);
- $vars[] = $node->expr;
} else {
- throw new Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $this->lineno);
+ $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());
}
}
throw new Twig_SyntaxError('When a plural is used, you must pass the count as an argument to the "trans" tag', $lineno);
}
}
+
$stream->expect(Twig_Token::BLOCK_END_TYPE);
- return new Twig_Node_Trans($count, $body, $plural, $lineno, $this->getTag());
+ $this->checkTransString($body, $lineno);
+
+ return new Twig_Node_Trans($body, $plural, $count, $lineno, $this->getTag());
}
public function decideForFork($token)
{
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);
+ }
+ }
}
new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
new Twig_Node_Text(' apples', 0),
), array(), 0);
- $node = new Twig_Node_Trans($count, $body, $plural, 0);
+ $node = new Twig_Node_Trans($body, $plural, $count, 0);
$this->assertEquals($body, $node->body);
$this->assertEquals($count, $node->count);
$this->assertEquals($plural, $node->plural);
}
- /**
- * @covers Twig_Node_Trans::compile
- * @covers Twig_Node_Trans::compileString
- * @dataProvider getTests
- */
- public function testCompile($node, $source, $environment = null)
- {
- parent::testCompile($node, $source, $environment);
-
- $body = new Twig_Node(array(
- new Twig_Node_Expression_Constant('Hello', 0),
- ), array(), 0);
- $node = new Twig_Node_Trans(null, $body, null, 0);
-
- try {
- $node->compile($this->getCompiler());
- $this->fail();
- } catch (Exception $e) {
- $this->assertEquals('Twig_SyntaxError', get_class($e));
- }
- }
-
public function getTests()
{
$tests = array();
$body = new Twig_Node(array(
new Twig_Node_Text('Hello', 0),
), array(), 0);
- $node = new Twig_Node_Trans(null, $body, null, 0);
+ $node = new Twig_Node_Trans($body, null, null, 0);
$tests[] = array($node, 'echo gettext("Hello");');
$body = new Twig_Node(array(
new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
new Twig_Node_Text(' pommes', 0),
), array(), 0);
- $node = new Twig_Node_Trans(null, $body, null, 0);
+ $node = new Twig_Node_Trans($body, null, null, 0);
$tests[] = array($node, 'echo strtr(gettext("J\'ai %foo% pommes"), array("%foo%" => (isset($context[\'foo\']) ? $context[\'foo\'] : null), ));');
$count = new Twig_Node_Expression_Constant(12, 0);
new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
new Twig_Node_Text(' apples', 0),
), array(), 0);
- $node = new Twig_Node_Trans($count, $body, $plural, 0);
+ $node = new Twig_Node_Trans($body, $plural, $count, 0);
$tests[] = array($node, 'echo strtr(ngettext("Hey %name%, I have one apple", "Hey %name%, I have %count% apples", abs(12)), array("%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%count%" => abs(12), ));');
return $tests;