Skip to content

Commit

Permalink
Merge branch '4.1.x'
Browse files Browse the repository at this point in the history
- Fixes #10
- Fixes #11

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Apr 19, 2024
2 parents 8e6df4e + 6981740 commit 02bc04f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 23 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

* Drop support for PHP 7.2, PHP 7.3, PHP 7.4 and PHP 8.0

## [4.1.1] - 2024-04-19

* Add support for Twig 3.9 (#12)

## [4.1.0] - 2023-09-12

* Drop support for PHP 7.1
Expand Down Expand Up @@ -39,7 +43,8 @@

* First release of this library.

[Unreleased]: https://github.com/phpmyadmin/twig-i18n-extension/compare/4.1.0...HEAD
[Unreleased]: https://github.com/phpmyadmin/twig-i18n-extension/compare/4.1.1...HEAD
[4.1.1]: https://github.com/phpmyadmin/twig-i18n-extension/compare/4.1.0...4.1.1
[4.1.0]: https://github.com/phpmyadmin/twig-i18n-extension/compare/v4.0.1...4.1.0
[4.0.1]: https://github.com/phpmyadmin/twig-i18n-extension/compare/v4.0.0...v4.0.1
[4.0.0]: https://github.com/phpmyadmin/twig-i18n-extension/compare/v3.0.0...v4.0.0
Expand Down
12 changes: 10 additions & 2 deletions src/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpMyAdmin\Twig\Extensions\Node;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\CheckToStringNode;
use Twig\Node\Expression\AbstractExpression;
Expand All @@ -26,6 +27,7 @@
use Twig\Node\TextNode;

use function array_merge;
use function class_exists;
use function count;
use function sprintf;
use function str_replace;
Expand All @@ -36,6 +38,7 @@
*
* Author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
#[YieldReady]
class TransNode extends Node
{
/**
Expand Down Expand Up @@ -126,7 +129,7 @@ public function compile(Compiler $compiler)

if ($vars) {
$compiler
->raw('echo strtr(' . $function . '(');
->raw($this->echoOrYield() . ' strtr(' . $function . '(');

if ($hasDomain) {
[$domain] = $this->compileString($this->getNode('domain'));
Expand Down Expand Up @@ -176,7 +179,7 @@ public function compile(Compiler $compiler)
$compiler->raw("));\n");
} else {
$compiler
->raw('echo ' . $function . '(');
->raw($this->echoOrYield() . ' ' . $function . '(');

if ($hasDomain) {
[$domain] = $this->compileString($this->getNode('domain'));
Expand Down Expand Up @@ -308,4 +311,9 @@ protected function getTransFunction(bool $hasPlural, bool $hasContext, bool $has
// gettext($msgid);
return $functionPrefix . ($hasContext ? 'pgettext' : 'gettext');
}

private function echoOrYield(): string
{
return class_exists(YieldReady::class) ? 'yield' : 'echo';
}
}
10 changes: 10 additions & 0 deletions src/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ protected function preParse(Token $token): array

$this->checkTransString($body, $lineno);

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());
}

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());
}

return [$body, $plural, $count, $context, $notes, $domain, $lineno, $this->getTag()];
}

Expand Down
10 changes: 10 additions & 0 deletions test/Fixtures/TransTagSetVariable.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test assigning to a variable with set
--TEMPLATE--
A {% set variable %}{% trans 'text' %}{% endset %} B {{ variable }} C
--DATA--
return []
--CONFIG--
return []
--EXPECT--
A B text C
33 changes: 25 additions & 8 deletions test/Node/MoTranslatorTransTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@
namespace PhpMyAdmin\Tests\Twig\Extensions\Node;

use PhpMyAdmin\Twig\Extensions\Node\TransNode;
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;

use function class_exists;
use function sprintf;

class MoTranslatorTransTest extends NodeTestCase
{
private function echoOrYield(): string
{
return class_exists(YieldReady::class) ? 'yield' : 'echo';
}

public static function setUpBeforeClass(): void
{
TransNode::$notesLabel = '// l10n: ';
Expand Down Expand Up @@ -80,7 +87,10 @@ public function getTests(): array
new TextNode('coredomain', 0),
], [], 0);
$node = new TransNode($body, null, null, null, null, $domain, 0);
$tests[] = [$node, sprintf('echo _dgettext("coredomain", %s);', $this->getVariableGetter('foo'))];
$tests[] = [
$node,
sprintf($this->echoOrYield() . ' _dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$body = new NameExpression('foo', 0);
$domain = new Node([
Expand All @@ -92,7 +102,10 @@ public function getTests(): array
$node = new TransNode($body, null, null, $context, null, $domain, 0);
$tests[] = [
$node,
sprintf('echo _dpgettext("coredomain", "The context", %s);', $this->getVariableGetter('foo')),
sprintf(
$this->echoOrYield() . ' _dpgettext("coredomain", "The context", %s);',
$this->getVariableGetter('foo'),
),
];

$body = new Node([
Expand All @@ -104,7 +117,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->echoOrYield() . ' strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -126,7 +139,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,'
$this->echoOrYield() . ' strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,'
. ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand All @@ -146,7 +159,8 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->echoOrYield()
. ' strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -171,7 +185,8 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,'
$this->echoOrYield()
. ' strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,'
. ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand All @@ -194,7 +209,8 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->echoOrYield()
. ' strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand Down Expand Up @@ -222,7 +238,8 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",'
$this->echoOrYield()
. ' strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",'
. ' "Hey %%name%%, I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand Down
41 changes: 29 additions & 12 deletions test/Node/TransTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace PhpMyAdmin\Tests\Twig\Extensions\Node;

use PhpMyAdmin\Twig\Extensions\Node\TransNode;
use Twig\Attribute\YieldReady;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
Expand All @@ -23,10 +24,16 @@
use Twig\Node\TextNode;
use Twig\Test\NodeTestCase;

use function class_exists;
use function sprintf;

class TransTest extends NodeTestCase
{
private function echoOrYield(): string
{
return class_exists(YieldReady::class) ? 'yield' : 'echo';
}

public function testConstructor(): void
{
$count = new ConstantExpression(12, 0);
Expand Down Expand Up @@ -89,7 +96,8 @@ public function testEnableDebugNotEnabled(): void
$this->assertEmpty($compiler->getDebugInfo());
$sourceCode = $compiler->compile($node)->getSource();
$this->assertSame(
'// custom: Notes for translators' . "\n" . 'echo strtr(ngettext("There is 1 pending task",'
'// custom: Notes for translators' . "\n"
. $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",'
. ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n",
$sourceCode,
);
Expand Down Expand Up @@ -117,7 +125,8 @@ public function testEnableDebugEnabled(): void
$this->assertEmpty($compiler->getDebugInfo());
$sourceCode = $compiler->compile($node)->getSource();
$this->assertSame(
'// line 80' . "\n" . '// custom: Notes for translators' . "\n" . 'echo strtr(ngettext("There'
'// line 80' . "\n" . '// custom: Notes for translators' . "\n"
. $this->echoOrYield() . ' strtr(ngettext("There'
. ' is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n",
$sourceCode,
);
Expand All @@ -136,21 +145,24 @@ public function getTests(): array
new TextNode('coredomain', 0),
], [], 0);
$node = new TransNode($body, null, null, null, null, $domain, 0);
$tests[] = [$node, sprintf('echo dgettext("coredomain", %s);', $this->getVariableGetter('foo'))];
$tests[] = [
$node,
sprintf($this->echoOrYield() . ' dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$body = new NameExpression('foo', 0);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [$node, sprintf('echo gettext(%s);', $this->getVariableGetter('foo'))];
$tests[] = [$node, sprintf($this->echoOrYield() . ' gettext(%s);', $this->getVariableGetter('foo'))];

$body = new ConstantExpression('Hello', 0);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [$node, 'echo gettext("Hello");'];
$tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");'];

$body = new Node([
new TextNode('Hello', 0),
], [], 0);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [$node, 'echo gettext("Hello");'];
$tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");'];

$body = new Node([
new TextNode('J\'ai ', 0),
Expand All @@ -161,7 +173,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -183,7 +195,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have'
$this->echoOrYield() . ' strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have'
. ' %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand All @@ -205,7 +217,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
'echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -214,12 +226,16 @@ public function getTests(): array
$body = new ConstantExpression('Hello', 0);
$notes = new TextNode('Notes for translators', 0);
$node = new TransNode($body, null, null, null, $notes, null, 0);
$tests[] = [$node, "// notes: Notes for translators\necho gettext(\"Hello\");"];
$tests[] = [$node, "// notes: Notes for translators\n" . $this->echoOrYield() . ' gettext("Hello");'];

$body = new ConstantExpression('Hello', 0);
$notes = new TextNode("Notes for translators\nand line breaks", 0);
$node = new TransNode($body, null, null, null, $notes, null, 0);
$tests[] = [$node, "// notes: Notes for translators and line breaks\necho gettext(\"Hello\");"];
$tests[] = [
$node,
"// notes: Notes for translators and line breaks\n"
. $this->echoOrYield() . ' gettext("Hello");',
];

$count = new ConstantExpression(5, 0);
$body = new TextNode('There is 1 pending task', 0);
Expand All @@ -232,7 +248,8 @@ public function getTests(): array
$node = new TransNode($body, $plural, $count, null, $notes, null, 0);
$tests[] = [
$node,
'// notes: Notes for translators' . "\n" . 'echo strtr(ngettext("There is 1 pending task",'
'// notes: Notes for translators' . "\n"
. $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",'
. ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));',
];

Expand Down

0 comments on commit 02bc04f

Please sign in to comment.