From 7c87855f5824dedc6be7b0845c7750a9a473d370 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 5 Apr 2022 22:24:08 +0200 Subject: [PATCH] added {translate} as pair replacement for {_} --- src/Latte/Macros/CoreMacros.php | 7 ++++++- tests/tags/translate.phpt | 18 +++++++++++------- tests/tags/translate.print.phpt | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 tests/tags/translate.print.phpt diff --git a/src/Latte/Macros/CoreMacros.php b/src/Latte/Macros/CoreMacros.php index fbc0ef349..3534c683c 100644 --- a/src/Latte/Macros/CoreMacros.php +++ b/src/Latte/Macros/CoreMacros.php @@ -71,6 +71,7 @@ public static function install(Latte\Compiler $compiler): void $me->addMacro('r', '?>}addMacro('_', [$me, 'macroTranslate'], [$me, 'macroTranslate']); + $me->addMacro('translate', [$me, 'macroTranslate'], [$me, 'macroTranslate']); $me->addMacro('=', [$me, 'macroExpr']); $me->addMacro('capture', [$me, 'macroCapture'], [$me, 'macroCaptureEnd']); @@ -337,6 +338,7 @@ public function macroRollback(MacroNode $node, PhpWriter $writer): string /** * {_$var |modifiers} + * {translate|modifiers} */ public function macroTranslate(MacroNode $node, PhpWriter $writer): string { @@ -362,8 +364,11 @@ public function macroTranslate(MacroNode $node, PhpWriter $writer): string implode('', $node->context) ); - } elseif ($node->empty = ($node->args !== '')) { + } elseif ($node->empty = ($node->args !== '') && $node->name === '_') { return $writer->write('echo %modify(($this->filters->translate)(%node.args)) %node.line;'); + + } elseif ($node->name === '_') { + trigger_error("As a pair tag for translation, {translate} ... {/translate} should be used instead of {_} ... {/} (on line $node->startLine)", E_USER_DEPRECATED); } return ''; diff --git a/tests/tags/translate.phpt b/tests/tags/translate.phpt index 35ec2e345..4628c594f 100644 --- a/tests/tags/translate.phpt +++ b/tests/tags/translate.phpt @@ -1,21 +1,25 @@ setLoader(new Latte\Loaders\StringLoader); -// {_...} -Assert::same('filters->translate)(\'var\')); ?>', $compiler->expandMacro('_', 'var', '')->openingCode); -Assert::same('filters->filter)(($this->filters->translate)(\'var\'))); ?>', $compiler->expandMacro('_', 'var', '|filter')->openingCode); +Assert::contains( + 'echo $this->filters->filterContent("translate", $ʟ_fi, \'abc\') /* line 1 */;', + $latte->compile('{translate}abc{/translate}') +); +Assert::contains( + 'echo $this->filters->filterContent(\'filter\', $ʟ_fi, $this->filters->filterContent("translate", $ʟ_fi, \'abc\')) /* line 1 */;', + $latte->compile('{translate|filter}abc{/translate}') +); diff --git a/tests/tags/translate.print.phpt b/tests/tags/translate.print.phpt new file mode 100644 index 000000000..fb3ae24fb --- /dev/null +++ b/tests/tags/translate.print.phpt @@ -0,0 +1,25 @@ +setLoader(new Latte\Loaders\StringLoader); + +Assert::contains( + 'echo LR\Filters::escapeHtmlText(($this->filters->translate)(\'var\')) /* line 1 */;', + $latte->compile('{_var}') +); +Assert::contains( + 'echo LR\Filters::escapeHtmlText(($this->filters->filter)(($this->filters->translate)(\'var\'))) /* line 1 */;', + $latte->compile('{_var|filter}') +);