From a831a22c8291638624b39a673d40935c854371e3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 30 May 2022 15:52:16 +0200 Subject: [PATCH] TranslatorExtension moved to Latte (BC break) partially revert "Latte bridge: added snippets & translated" This reverts commit 16b7079cdadfcc01ff8a504c23bbed22c78086ac. --- .../ApplicationLatte/Nodes/TranslateNode.php | 107 ------------------ src/Bridges/ApplicationLatte/Template.php | 2 +- .../ApplicationLatte/TranslatorExtension.php | 92 --------------- tests/Bridges.Latte3/{_var}.phpt | 66 ----------- tests/Bridges.Latte3/{translate}.phpt | 100 ---------------- 5 files changed, 1 insertion(+), 366 deletions(-) delete mode 100644 src/Bridges/ApplicationLatte/Nodes/TranslateNode.php delete mode 100644 src/Bridges/ApplicationLatte/TranslatorExtension.php delete mode 100644 tests/Bridges.Latte3/{_var}.phpt delete mode 100644 tests/Bridges.Latte3/{translate}.phpt diff --git a/src/Bridges/ApplicationLatte/Nodes/TranslateNode.php b/src/Bridges/ApplicationLatte/Nodes/TranslateNode.php deleted file mode 100644 index 48b2c707d..000000000 --- a/src/Bridges/ApplicationLatte/Nodes/TranslateNode.php +++ /dev/null @@ -1,107 +0,0 @@ - */ - public static function create(Tag $tag, ?Translator $translator): \Generator - { - $tag->outputMode = $tag::OutputKeepIndentation; - - $node = new static; - $args = $tag->parser->parseArguments(); - $node->modifier = $tag->parser->parseModifier(); - $node->modifier->escape = true; - if ($tag->void) { - return new NopNode; - } - - [$node->content] = yield; - - if ($text = NodeHelpers::toText($node->content)) { - $node->content = new TextNode($text); - if ($translator) { - try { - $translation = $translator->translate($text, ...NodeHelpers::toValue($args, constants: true)); - if (is_string($translation)) { - $node->content = new TextNode($translation); - return $node; - } - } catch (\InvalidArgumentException) { - } - } - } - - array_unshift($node->modifier->filters, new Php\FilterNode(new Php\IdentifierNode('translate'), $args->toArguments())); - - return $node; - } - - - public function print(PrintContext $context): string - { - if ($this->content instanceof TextNode) { - return $context->format( - <<<'XX' - $ʟ_fi = new LR\FilterInfo(%dump); - echo %modifyContent(%dump) %line; - XX, - $context->getEscaper()->export(), - $this->modifier, - $this->content->content, - $this->position, - ); - - } else { - return $context->format( - <<<'XX' - ob_start(fn() => ''); try { - %node - } finally { - $ʟ_tmp = ob_get_clean(); - } - $ʟ_fi = new LR\FilterInfo(%dump); - echo %modifyContent($ʟ_tmp) %line; - XX, - $this->content, - $context->getEscaper()->export(), - $this->modifier, - $this->position, - ); - } - } - - - public function &getIterator(): \Generator - { - yield $this->content; - yield $this->modifier; - } -} diff --git a/src/Bridges/ApplicationLatte/Template.php b/src/Bridges/ApplicationLatte/Template.php index 2f599572a..fbebb4bd2 100644 --- a/src/Bridges/ApplicationLatte/Template.php +++ b/src/Bridges/ApplicationLatte/Template.php @@ -117,7 +117,7 @@ function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator): string { } ); } else { - $this->latte->addExtension(new TranslatorExtension($translator, $language)); + $this->latte->addExtension(new Latte\Essential\TranslatorExtension($translator, $language)); } return $this; } diff --git a/src/Bridges/ApplicationLatte/TranslatorExtension.php b/src/Bridges/ApplicationLatte/TranslatorExtension.php deleted file mode 100644 index 2eeb52261..000000000 --- a/src/Bridges/ApplicationLatte/TranslatorExtension.php +++ /dev/null @@ -1,92 +0,0 @@ - [$this, 'parseTranslate'], - 'translate' => fn(Tag $tag): \Generator => Nodes\TranslateNode::create($tag, $this->key ? $this->translator : null), - ]; - } - - - public function getFilters(): array - { - return [ - 'translate' => fn(Latte\Runtime\FilterInfo $fi, ...$args): string => $this->translator === null - ? $args[0] - : $this->translator->translate(...$args), - ]; - } - - - public function getCacheKey(Engine $engine): mixed - { - return $this->key; - } - - - /** - * {_ ...} - */ - public function parseTranslate(Tag $tag): PrintNode - { - $tag->outputMode = $tag::OutputKeepIndentation; - $tag->expectArguments(); - $node = new PrintNode; - $node->expression = $tag->parser->parseExpression(); - $args = new Php\Expression\ArrayNode; - if ($tag->parser->stream->tryConsume(',')) { - $args = $tag->parser->parseArguments(); - } - - $node->modifier = $tag->parser->parseModifier(); - $node->modifier->escape = true; - - if ($this->translator && $this->key) { - try { - $translation = $this->translator->translate( - NodeHelpers::toValue($node->expression, constants: true), - ...NodeHelpers::toValue($args, constants: true), - ); - if (is_string($translation)) { - $node->expression = new Php\Scalar\StringNode($translation); - return $node; - } - } catch (\InvalidArgumentException) { - } - } - - array_unshift($node->modifier->filters, new Php\FilterNode(new Php\IdentifierNode('translate'), $args->toArguments())); - return $node; - } -} diff --git a/tests/Bridges.Latte3/{_var}.phpt b/tests/Bridges.Latte3/{_var}.phpt deleted file mode 100644 index fc5d1c4dd..000000000 --- a/tests/Bridges.Latte3/{_var}.phpt +++ /dev/null @@ -1,66 +0,0 @@ -setLoader(new Latte\Loaders\StringLoader); -$latte->addExtension(new Nette\Bridges\ApplicationLatte\TranslatorExtension(null)); - -Assert::contains( - 'echo LR\Filters::escapeHtmlText(($this->filters->translate)(\'var\')) /*', - $latte->compile('{_var}'), -); - -Assert::contains( - 'echo LR\Filters::escapeHtmlText(($this->filters->filter)(($this->filters->translate)(\'var\'))) /*', - $latte->compile('{_var|filter}'), -); - -Assert::contains( - 'echo LR\Filters::escapeHtmlText(($this->filters->translate)($var, 10, 20)) /* line 1 */;', - $latte->compile('{_$var, 10, 20}'), -); - - -class MyTranslator implements Translator -{ - public function translate($message, ...$parameters): string - { - return strrev($message) . implode(',', $parameters); - } -} - -$latte = new Latte\Engine; -$latte->setLoader(new Latte\Loaders\StringLoader); -$latte->addExtension(new Nette\Bridges\ApplicationLatte\TranslatorExtension(new MyTranslator)); -Assert::contains( - 'echo LR\Filters::escapeHtmlText(($this->filters->translate)(\'a&b\', 1, 2))', - $latte->compile('{_"a&b", 1, 2}'), -); -Assert::same( - 'b&a1,2', - $latte->renderToString('{_"a&b", 1, 2}'), -); - - -$latte->addExtension(new Nette\Bridges\ApplicationLatte\TranslatorExtension(new MyTranslator, 'en')); -Assert::contains( - 'echo LR\Filters::escapeHtmlText(\'b&a1,2\')', - $latte->compile('{_"a&b", 1, 2}'), -); -Assert::same( - 'b&a1,2', - $latte->renderToString('{_"a&b", 1, 2}'), -); diff --git a/tests/Bridges.Latte3/{translate}.phpt b/tests/Bridges.Latte3/{translate}.phpt deleted file mode 100644 index 26397f8d8..000000000 --- a/tests/Bridges.Latte3/{translate}.phpt +++ /dev/null @@ -1,100 +0,0 @@ -setLoader(new Latte\Loaders\StringLoader); -$latte->addExtension(new Nette\Bridges\ApplicationLatte\TranslatorExtension(null)); - -Assert::match( - <<<'XX' - %A% - $ʟ_fi = new LR\FilterInfo('html'); - echo LR\Filters::convertTo($ʟ_fi, 'html', $this->filters->filterContent('translate', $ʟ_fi, 'abc')) /* line 1 */; - %A% - XX, - $latte->compile('{translate}abc{/translate}'), -); - -Assert::contains( - 'echo LR\Filters::convertTo($ʟ_fi, \'html\', $this->filters->filterContent(\'translate\', $ʟ_fi, \'abc\', 10, 20)) /* line 1 */;', - $latte->compile('{translate 10, 20}abc{/translate}'), -); - -Assert::match( - <<<'XX' - %A% - $ʟ_fi = new LR\FilterInfo('html'); - echo LR\Filters::convertTo($ʟ_fi, 'html', $this->filters->filterContent('filter', $ʟ_fi, $this->filters->filterContent('translate', $ʟ_fi, 'abc'))) /* line 1 */; - %A% - XX, - $latte->compile('{translate|filter}abc{/translate}'), -); - -Assert::match( - <<<'XX' - %A% - ob_start(fn() => ''); - try { - if (true) /* line 1 */ { - echo 'abc'; - } - - } finally { - $ʟ_tmp = ob_get_clean(); - } - $ʟ_fi = new LR\FilterInfo('html'); - echo LR\Filters::convertTo($ʟ_fi, 'html', $this->filters->filterContent('translate', $ʟ_fi, $ʟ_tmp)) /* line 1 */; - %A% - XX, - $latte->compile('{translate}{if true}abc{/if}{/translate}'), -); - -Assert::notContains( - "'translate'", - $latte->compile('{translate /}'), -); - - -class MyTranslator implements Translator -{ - public function translate($message, ...$parameters): string - { - return strrev($message) . implode(',', $parameters); - } -} - -$latte = new Latte\Engine; -$latte->setLoader(new Latte\Loaders\StringLoader); -$latte->addExtension(new Nette\Bridges\ApplicationLatte\TranslatorExtension(new MyTranslator)); -Assert::contains( - 'echo LR\Filters::convertTo($ʟ_fi, \'html\', $this->filters->filterContent(\'translate\', $ʟ_fi, \'a&b\', 1, 2))', - $latte->compile('{translate 1,2}a&b{/translate}'), -); -Assert::same( - 'b&a1,2', - $latte->renderToString('{translate 1,2}a&b{/translate}'), -); - - -$latte->addExtension(new Nette\Bridges\ApplicationLatte\TranslatorExtension(new MyTranslator, 'en')); -Assert::contains( - 'echo LR\Filters::convertTo($ʟ_fi, \'html\', \'b&a1,2\')', - $latte->compile('{translate 1,2}a&b{/translate}'), -); -Assert::same( - 'b&a1,2', - $latte->renderToString('{translate 1,2}a&b{/translate}'), -);