diff --git a/src/Node/I18nNode.php b/src/Node/I18nNode.php new file mode 100644 index 0000000..fbde515 --- /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|null $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 d016afc..5ba9735 100644 --- a/src/Node/TransNode.php +++ b/src/Node/TransNode.php @@ -253,7 +253,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 44fd954..1e1c830 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\AbstractExpression; @@ -96,12 +97,12 @@ protected function preParse(Token $token): array if ($notes instanceof TextNode) { // Don't use TextNode for $notes to avoid it getting merged with $body when optimizing. - $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 when optimizing. - $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 d7e6092..56a6c85 100644 --- a/test/Node/MoTranslatorTransTest.php +++ b/test/Node/MoTranslatorTransTest.php @@ -17,7 +17,7 @@ use PhpMyAdmin\Twig\Extensions\Node\TransNode; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\NameExpression; -use Twig\Node\Node; +use Twig\Node\Nodes; use Twig\Node\PrintNode; use Twig\Node\TextNode; use Twig\Test\NodeTestCase; @@ -41,25 +41,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')); @@ -76,9 +76,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, @@ -86,12 +86,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, @@ -101,11 +101,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, @@ -116,18 +116,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, @@ -140,14 +140,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, @@ -158,21 +158,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, @@ -185,17 +185,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, @@ -206,24 +206,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/TransTest.php b/test/Node/TransTest.php index b051a9d..b02d96c 100644 --- a/test/Node/TransTest.php +++ b/test/Node/TransTest.php @@ -18,7 +18,7 @@ use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\FilterExpression; use Twig\Node\Expression\NameExpression; -use Twig\Node\Node; +use Twig\Node\Nodes; use Twig\Node\PrintNode; use Twig\Node\TextNode; use Twig\Test\NodeTestCase; @@ -30,16 +30,16 @@ class TransTest extends NodeTestCase 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')); @@ -50,19 +50,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')); @@ -75,11 +75,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: '; @@ -103,11 +103,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; @@ -134,9 +134,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, @@ -151,17 +151,17 @@ public function getTests(): array $node = new TransNode($body, null, null, null, null, null, 0); $tests[] = [$node, 'yield 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, 'yield 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, @@ -172,18 +172,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, @@ -197,14 +197,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[] = [ @@ -232,11 +232,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[] = [