diff --git a/src/Node/I18nNode.php b/src/Node/I18nNode.php new file mode 100644 index 0000000..6e1c153 --- /dev/null +++ b/src/Node/I18nNode.php @@ -0,0 +1,34 @@ + $attributes An array of attributes (should not be nodes) + * @param int $lineno The line number + */ + public function __construct(?Node $node, array $attributes, int $lineno) + { + parent::__construct($node === null ? [] : [$node], $attributes, $lineno); + } +} diff --git a/src/Node/TransNode.php b/src/Node/TransNode.php index d06d891..81a6e08 100644 --- a/src/Node/TransNode.php +++ b/src/Node/TransNode.php @@ -23,6 +23,7 @@ use Twig\Node\Expression\FilterExpression; use Twig\Node\Expression\NameExpression; use Twig\Node\Expression\TempNameExpression; +use Twig\Node\Expression\Variable\ContextVariable; use Twig\Node\Node; use Twig\Node\PrintNode; use Twig\Node\TextNode; @@ -259,7 +260,11 @@ protected function compileString(Node $body): array $attributeName = $n->getAttribute('name'); $msg .= sprintf('%%%s%%', $attributeName); - $vars[] = new NameExpression($attributeName, $n->getTemplateLine()); + if (class_exists(ContextVariable::class)) { + $vars[] = new ContextVariable($attributeName, $n->getTemplateLine()); + } else { + $vars[] = new NameExpression($attributeName, $n->getTemplateLine()); + } } else { /** @phpstan-var TextNode $node */ $msg .= $node->getAttribute('data'); @@ -269,7 +274,7 @@ protected function compileString(Node $body): array $msg = $body->getAttribute('data'); } - return [new Node([new ConstantExpression(trim($msg), $body->getTemplateLine())]), $vars]; + return [new I18nNode(new ConstantExpression(trim($msg), $body->getTemplateLine()), [], 0), $vars]; } /** diff --git a/src/TokenParser/TransTokenParser.php b/src/TokenParser/TransTokenParser.php index 6ba8510..2c5c1be 100644 --- a/src/TokenParser/TransTokenParser.php +++ b/src/TokenParser/TransTokenParser.php @@ -14,6 +14,7 @@ namespace PhpMyAdmin\Twig\Extensions\TokenParser; +use PhpMyAdmin\Twig\Extensions\Node\I18nNode; use PhpMyAdmin\Twig\Extensions\Node\TransNode; use Twig\Error\SyntaxError; use Twig\Node\Expression\NameExpression; @@ -102,12 +103,12 @@ protected function preParse(Token $token): array if ($notes instanceof TextNode) { // Don't use TextNode for $notes to avoid it getting merged with $body in Twig >= 3.9.0. - $notes = new Node([], ['data' => $notes->getAttribute('data')], $notes->getTemplateLine()); + $notes = new I18nNode(null, ['data' => $notes->getAttribute('data')], $notes->getTemplateLine()); } if ($context instanceof TextNode) { // Don't use TextNode for $context to avoid it getting merged with $body in Twig >= 3.9.0. - $context = new Node([], ['data' => $context->getAttribute('data')], $context->getTemplateLine()); + $context = new I18nNode(null, ['data' => $context->getAttribute('data')], $context->getTemplateLine()); } return [$body, $plural, $count, $context, $notes, $domain, $lineno, $this->getTag()]; diff --git a/test/Node/MoTranslatorTransTest.php b/test/Node/MoTranslatorTransTest.php index 72eac84..8596d41 100644 --- a/test/Node/MoTranslatorTransTest.php +++ b/test/Node/MoTranslatorTransTest.php @@ -18,7 +18,6 @@ use Twig\Attribute\YieldReady; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\NameExpression; -use Twig\Node\Node; use Twig\Node\PrintNode; use Twig\Node\TextNode; use Twig\Test\NodeTestCase; @@ -48,25 +47,25 @@ public static function tearDownAfterClass(): void public function testFullConstructor(): void { $count = new ConstantExpression(12, 0); - $body = new Node([ + $body = new Nodes([ new TextNode('Hello', 0), - ], [], 0); - $notes = new Node([ + ]); + $notes = new Nodes([ new TextNode('notes for translators', 0), - ], [], 0); - $domain = new Node([ + ]); + $domain = new Nodes([ new TextNode('mydomain', 0), - ], [], 0); - $context = new Node([ + ]); + $context = new Nodes([ new TextNode('mydomain', 0), - ], [], 0); - $plural = new Node([ + ]); + $plural = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' apples', 0), - ], [], 0); + ]); $node = new TransNode($body, $plural, $count, $context, $notes, $domain, 0); $this->assertEquals($body, $node->getNode('body')); @@ -85,9 +84,9 @@ public function getTests(): array $tests = []; $body = new NameExpression('foo', 0); - $domain = new Node([ + $domain = new Nodes([ new TextNode('coredomain', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, null, null, $domain, 0); $tests[] = [ $node, @@ -95,12 +94,12 @@ public function getTests(): array ]; $body = new NameExpression('foo', 0); - $domain = new Node([ + $domain = new Nodes([ new TextNode('coredomain', 0), - ], [], 0); - $context = new Node([ + ]); + $context = new Nodes([ new TextNode('The context', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, $context, null, $domain, 0); $tests[] = [ $node, @@ -110,11 +109,11 @@ public function getTests(): array ), ]; - $body = new Node([ + $body = new Nodes([ new TextNode('J\'ai ', 0), new PrintNode(new NameExpression('foo', 0), 0), new TextNode(' pommes', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, null, null, null, 0); $tests[] = [ $node, @@ -125,18 +124,18 @@ public function getTests(): array ]; $count = new ConstantExpression(12, 0); - $body = new Node([ + $body = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have one apple', 0), - ], [], 0); - $plural = new Node([ + ]); + $plural = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' apples', 0), - ], [], 0); + ]); $node = new TransNode($body, $plural, $count, null, null, null, 0); $tests[] = [ $node, @@ -149,14 +148,14 @@ public function getTests(): array ), ]; - $body = new Node([ + $body = new Nodes([ new TextNode('J\'ai ', 0), new PrintNode(new NameExpression('foo', 0), 0), new TextNode(' pommes', 0), - ], [], 0); - $context = new Node([ + ]); + $context = new Nodes([ new TextNode('The context', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, $context, null, null, 0); $tests[] = [ $node, @@ -168,21 +167,21 @@ public function getTests(): array ]; $count = new ConstantExpression(12, 0); - $body = new Node([ + $body = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have one apple', 0), - ], [], 0); - $context = new Node([ + ]); + $context = new Nodes([ new TextNode('The context', 0), - ], [], 0); - $plural = new Node([ + ]); + $plural = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' apples', 0), - ], [], 0); + ]); $node = new TransNode($body, $plural, $count, $context, null, null, 0); $tests[] = [ $node, @@ -196,17 +195,17 @@ public function getTests(): array ), ]; - $body = new Node([ + $body = new Nodes([ new TextNode('J\'ai ', 0), new PrintNode(new NameExpression('foo', 0), 0), new TextNode(' pommes', 0), - ], [], 0); - $context = new Node([ + ]); + $context = new Nodes([ new TextNode('The context', 0), - ], [], 0); - $domain = new Node([ + ]); + $domain = new Nodes([ new TextNode('mydomain', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, $context, null, $domain, 0); $tests[] = [ $node, @@ -218,24 +217,24 @@ public function getTests(): array ]; $count = new ConstantExpression(12, 0); - $body = new Node([ + $body = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have one apple', 0), - ], [], 0); - $context = new Node([ + ]); + $context = new Nodes([ new TextNode('The context', 0), - ], [], 0); - $domain = new Node([ + ]); + $domain = new Nodes([ new TextNode('mydomain', 0), - ], [], 0); - $plural = new Node([ + ]); + $plural = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' apples', 0), - ], [], 0); + ]); $node = new TransNode($body, $plural, $count, $context, null, $domain, 0); $tests[] = [ $node, diff --git a/test/Node/Nodes.php b/test/Node/Nodes.php new file mode 100644 index 0000000..90e2d99 --- /dev/null +++ b/test/Node/Nodes.php @@ -0,0 +1,31 @@ + $nodes */ + public function __construct(array $nodes = [], int $lineno = 0) + { + parent::__construct($nodes, [], $lineno); + } +} diff --git a/test/Node/TransTest.php b/test/Node/TransTest.php index 396f7d5..b5bda28 100644 --- a/test/Node/TransTest.php +++ b/test/Node/TransTest.php @@ -19,7 +19,6 @@ use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\FilterExpression; use Twig\Node\Expression\NameExpression; -use Twig\Node\Node; use Twig\Node\PrintNode; use Twig\Node\TextNode; use Twig\Test\NodeTestCase; @@ -37,16 +36,16 @@ private static function echoOrYield(): string public function testConstructor(): void { $count = new ConstantExpression(12, 0); - $body = new Node([ + $body = new Nodes([ new TextNode('Hello', 0), - ], [], 0); - $plural = new Node([ + ]); + $plural = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' apples', 0), - ], [], 0); + ]); $node = new TransNode($body, $plural, $count, null, null, null, 0); $this->assertEquals($body, $node->getNode('body')); @@ -57,19 +56,19 @@ public function testConstructor(): void public function testConstructorWithDomain(): void { $count = new ConstantExpression(12, 0); - $body = new Node([ + $body = new Nodes([ new TextNode('Hello', 0), - ], [], 0); - $domain = new Node([ + ]); + $domain = new Nodes([ new TextNode('coredomain', 0), - ], [], 0); - $plural = new Node([ + ]); + $plural = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' apples', 0), - ], [], 0); + ]); $node = new TransNode($body, $plural, $count, null, null, $domain, 0); $this->assertEquals($body, $node->getNode('body')); @@ -82,11 +81,11 @@ public function testEnableDebugNotEnabled(): void { $count = new ConstantExpression(5, 0); $body = new TextNode('There is 1 pending task', 0); - $plural = new Node([ + $plural = new Nodes([ new TextNode('There are ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' pending tasks', 0), - ], [], 0); + ]); $notes = new TextNode('Notes for translators', 0); TransNode::$enableAddDebugInfo = false; TransNode::$notesLabel = '// custom: '; @@ -110,11 +109,11 @@ public function testEnableDebugEnabled(): void { $count = new ConstantExpression(5, 0); $body = new TextNode('There is 1 pending task', 0); - $plural = new Node([ + $plural = new Nodes([ new TextNode('There are ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' pending tasks', 0), - ], [], 0); + ]); $notes = new TextNode('Notes for translators', 0); TransNode::$enableAddDebugInfo = true; @@ -143,9 +142,9 @@ public function getTests(): array $tests = []; $body = new NameExpression('foo', 0); - $domain = new Node([ + $domain = new Nodes([ new TextNode('coredomain', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, null, null, $domain, 0); $tests[] = [ $node, @@ -160,17 +159,17 @@ public function getTests(): array $node = new TransNode($body, null, null, null, null, null, 0); $tests[] = [$node, self::echoOrYield() . ' gettext("Hello");']; - $body = new Node([ + $body = new Nodes([ new TextNode('Hello', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, null, null, null, 0); $tests[] = [$node, self::echoOrYield() . ' gettext("Hello");']; - $body = new Node([ + $body = new Nodes([ new TextNode('J\'ai ', 0), new PrintNode(new NameExpression('foo', 0), 0), new TextNode(' pommes', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, null, null, null, 0); $tests[] = [ $node, @@ -181,18 +180,18 @@ public function getTests(): array ]; $count = new ConstantExpression(12, 0); - $body = new Node([ + $body = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have one apple', 0), - ], [], 0); - $plural = new Node([ + ]); + $plural = new Nodes([ new TextNode('Hey ', 0), new PrintNode(new NameExpression('name', 0), 0), new TextNode(', I have ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' apples', 0), - ], [], 0); + ]); $node = new TransNode($body, $plural, $count, null, null, null, 0); $tests[] = [ $node, @@ -206,14 +205,14 @@ public function getTests(): array ]; // with escaper extension set to on - $body = new Node([ + $body = new Nodes([ new TextNode('J\'ai ', 0), new PrintNode( - new FilterExpression(new NameExpression('foo', 0), new ConstantExpression('escape', 0), new Node(), 0), + new FilterExpression(new NameExpression('foo', 0), new ConstantExpression('escape', 0), new Nodes(), 0), 0 ), new TextNode(' pommes', 0), - ], [], 0); + ]); $node = new TransNode($body, null, null, null, null, null, 0); $tests[] = [ @@ -241,11 +240,11 @@ public function getTests(): array $count = new ConstantExpression(5, 0); $body = new TextNode('There is 1 pending task', 0); - $plural = new Node([ + $plural = new Nodes([ new TextNode('There are ', 0), new PrintNode(new NameExpression('count', 0), 0), new TextNode(' pending tasks', 0), - ], [], 0); + ]); $notes = new TextNode('Notes for translators', 0); $node = new TransNode($body, $plural, $count, null, $notes, null, 0); $tests[] = [