diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index 7b9ab9a56d6c..2a956830df98 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -262,6 +262,16 @@ protected function makeReplacements($line, array $replace) $shouldReplace = []; foreach ($replace as $key => $value) { + if ($value instanceof Closure) { + $line = preg_replace_callback( + '/<'.$key.'>(.*?)<\/'.$key.'>/', + fn ($args) => $value($args[1]), + $line + ); + + continue; + } + if (is_object($value) && isset($this->stringableHandlers[get_class($value)])) { $value = call_user_func($this->stringableHandlers[get_class($value)], $value); } diff --git a/tests/Translation/TranslationTranslatorTest.php b/tests/Translation/TranslationTranslatorTest.php index 07ec429aab77..6bf8092a6464 100755 --- a/tests/Translation/TranslationTranslatorTest.php +++ b/tests/Translation/TranslationTranslatorTest.php @@ -257,6 +257,42 @@ public function testGetJsonReplacesWithStringable() ); } + public function testTagReplacements() + { + $t = new Translator($this->getLoader(), 'en'); + + $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]); + $t->getLoader()->shouldReceive('load')->once()->with('en', 'We have some nice documentation', '*')->andReturn([]); + + $this->assertSame( + 'We have some nice documentation', + $t->get( + 'We have some nice documentation', + [ + "docs-link" => fn ($children) => "$children" + ] + ) + ); + } + + public function testTagReplacementsHandleMultipleOfSameTag() + { + $t = new Translator($this->getLoader(), 'en'); + + $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]); + $t->getLoader()->shouldReceive('load')->once()->with('en', 'bold something else also bold', '*')->andReturn([]); + + $this->assertSame( + 'bold something else also bold', + $t->get( + 'bold something else also bold', + [ + "bold-this" => fn ($children) => "$children" + ] + ) + ); + } + public function testDetermineLocalesUsingMethod() { $t = new Translator($this->getLoader(), 'en');