Skip to content

Commit

Permalink
Macro fix (#71)
Browse files Browse the repository at this point in the history
* Update Macros.php

* Update TranslatorTest.phpt

* Update Macros.php

* Update TranslatorTest.phpt

* Update Macros.php
  • Loading branch information
aleswita authored Jul 26, 2021
1 parent 2608274 commit 8c4c1e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/Latte/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ public function macroTranslate(
$latteProp = '$ʟ_fi';
}

return $writer->write($latteProp . ' = new LR\FilterInfo(%var); echo %modifyContent($this->filters->filterContent("translate", ' . $latteProp . ', %raw))', $node->context[0], $value);
return $writer->write(sprintf('%s = new LR\FilterInfo(%%var); echo %%modifyContent($this->filters->filterContent("translate", %s, %%raw))', $latteProp, $latteProp), $node->context[0], $value);
}

if ($node->empty = ($node->args !== '')) {
$messageProp = Helpers::createLatteProperty('Message');
$prefixProp = Helpers::createLatteProperty('Prefix');

$prefix = $messageProp . ' = isset(' . $prefixProp . ') && !\Contributte\Translation\Helpers::isAbsoluteMessage(%node.word) ? implode(".", ' . $prefixProp . ') . "." : "";';
$macroCodeEcho = Helpers::macroWithoutParameters($node)
? sprintf('echo %%modify(call_user_func($this->filters->translate, %s))', $messageProp)
: sprintf('echo %%modify(call_user_func($this->filters->translate, %s, %%node.args))', $messageProp);

if (Helpers::macroWithoutParameters($node)) {
return $writer->write($prefix . 'echo %modify(call_user_func($this->filters->translate, ' . $messageProp . ' . %node.word))');
}
return $writer->write(sprintf('
%s = %%node.word;
if (is_string(%s)) {
%s = isset(%s) && !\Contributte\Translation\Helpers::isAbsoluteMessage(%%node.word) ? implode(".", %s) . "." . %%node.word : %%node.word;
}
return $writer->write($prefix . 'echo %modify(call_user_func($this->filters->translate, ' . $messageProp . ' . %node.word, %node.args))');
%s;
', $messageProp, $messageProp, $messageProp, $prefixProp, $prefixProp, $macroCodeEcho));
}

return '';
Expand All @@ -92,7 +98,7 @@ public function macroPrefix(

if ($node->closing) {
if ($node->content !== null && $node->content !== '') {
return $writer->write($prefixProp . ' = array_pop(' . $tempPrefixProp . ');');
return $writer->write(sprintf('%s = array_pop(%s);', $prefixProp, $tempPrefixProp));
}

return '';
Expand All @@ -102,17 +108,17 @@ public function macroPrefix(
throw new CompileException('Expected message prefix, none given.');
}

return $writer->write('
if (!isset(' . $tempPrefixProp . ')) {
' . $tempPrefixProp . ' = [];
return $writer->write(sprintf('
if (!isset(%s)) {
%s = [];
}
if (isset(' . $prefixProp . ')) {
' . $tempPrefixProp . '[] = ' . $prefixProp . ';
if (isset(%s)) {
%s[] = %s;
}
' . $prefixProp . ' = [%node.word];
');
%s = [%%node.word];
', $tempPrefixProp, $tempPrefixProp, $prefixProp, $tempPrefixProp, $prefixProp, $prefixProp));
}

}
2 changes: 2 additions & 0 deletions tests/Tests/TranslatorTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ final class TranslatorTest extends TestAbstract
$latte->renderToString(FileMock::create('{translator}{_depth.message}{/translator}'));
}, CompileException::class);
Assert::same('<div>Hello</div>', $latte->renderToString(FileMock::create('<div n:translator="some.prefix">{_//messages.hello}</div>')));
Assert::same('Do not translate!', $latte->renderToString(FileMock::create('{php $wrapper = new \Contributte\Translation\Wrappers\NotTranslate("Do not translate!")}{_$wrapper}')));
Assert::same('Hi Ales!', $latte->renderToString(FileMock::create('{php $wrapper = new \Contributte\Translation\Wrappers\Message("hi", ["name" => "Ales"])}{_$wrapper}')));
}

public function test04(): void
Expand Down

0 comments on commit 8c4c1e8

Please sign in to comment.