mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 12:26:30 +00:00
fixed trans tag when used with the Escaper extension (refs #75)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 0.9.8
|
||||
|
||||
* fixed trans tag when used with the Escaper extension
|
||||
* fixed default cache umask
|
||||
* removed Twig_Template instances from the debug tag output
|
||||
* fixed objects with __isset() defined
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -95,11 +95,13 @@ class Twig_Node_Trans extends Twig_Node
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,9 +37,12 @@ class Twig_TokenParser_Trans extends Twig_TokenParser
|
||||
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)
|
||||
@@ -61,4 +64,19 @@ class Twig_TokenParser_Trans extends Twig_TokenParser
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,35 +29,13 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase
|
||||
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();
|
||||
@@ -65,7 +43,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase
|
||||
$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(
|
||||
@@ -73,7 +51,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase
|
||||
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);
|
||||
@@ -89,7 +67,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user