Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Node/I18nNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of Twig I18n extension.
*
* (c) 2025 phpMyAdmin contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PhpMyAdmin\Twig\Extensions\Node;

use Twig\Attribute\YieldReady;
use Twig\Node\Node;

/**
* Represents a single node
*/
#[YieldReady]
final class I18nNode extends Node
{
/**
* @param Node|null $node A single node
* @param array<string, mixed> $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);
}
}
9 changes: 7 additions & 2 deletions src/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()];
Expand Down
93 changes: 46 additions & 47 deletions test/Node/MoTranslatorTransTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
Expand All @@ -85,22 +84,22 @@ 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,
sprintf(self::echoOrYield() . ' _dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions test/Node/Nodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) phpMyAdmin contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PhpMyAdmin\Tests\Twig\Extensions\Node;

use Twig\Attribute\YieldReady;
use Twig\Node\Node;

/**
* Represents a list of nodes.
*/
#[YieldReady]
final class Nodes extends Node
{
/** @param array<string|int, Node> $nodes */
public function __construct(array $nodes = [], int $lineno = 0)
{
parent::__construct($nodes, [], $lineno);
}
}
Loading