Skip to content

Commit

Permalink
added {translate} as pair replacement for {_}
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 7, 2022
1 parent f329da9 commit 7c87855
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/Latte/Macros/CoreMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static function install(Latte\Compiler $compiler): void
$me->addMacro('r', '?>}<?php');

$me->addMacro('_', [$me, 'macroTranslate'], [$me, 'macroTranslate']);
$me->addMacro('translate', [$me, 'macroTranslate'], [$me, 'macroTranslate']);
$me->addMacro('=', [$me, 'macroExpr']);

$me->addMacro('capture', [$me, 'macroCapture'], [$me, 'macroCaptureEnd']);
Expand Down Expand Up @@ -337,6 +338,7 @@ public function macroRollback(MacroNode $node, PhpWriter $writer): string

/**
* {_$var |modifiers}
* {translate|modifiers}
*/
public function macroTranslate(MacroNode $node, PhpWriter $writer): string
{
Expand All @@ -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 '';
Expand Down
18 changes: 11 additions & 7 deletions tests/tags/translate.phpt
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?php

/**
* Test: Latte\Macros\CoreMacros: {_translate}
* Test: Latte\Macros\CoreMacros: {translate}
*/

declare(strict_types=1);

use Latte\Macros\CoreMacros;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$compiler = new Latte\Compiler;
CoreMacros::install($compiler);
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

// {_...}
Assert::same('<?php echo LR\Filters::escapeHtmlText(($this->filters->translate)(\'var\')); ?>', $compiler->expandMacro('_', 'var', '')->openingCode);
Assert::same('<?php echo LR\Filters::escapeHtmlText(($this->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}')
);
25 changes: 25 additions & 0 deletions tests/tags/translate.print.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Test: Latte\Macros\CoreMacros: {_}
*/

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->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}')
);

0 comments on commit 7c87855

Please sign in to comment.