From b7df00dd1f013ca49f1a29aadd5477c881c3e4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 16 Mar 2021 21:14:39 +0100 Subject: [PATCH] Improve callable strings --- src/GlossTranslator.php | 5 ++++- tests/GlossTest.php | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/GlossTranslator.php b/src/GlossTranslator.php index d95e1f3..f869396 100644 --- a/src/GlossTranslator.php +++ b/src/GlossTranslator.php @@ -228,7 +228,10 @@ class GlossTranslator extends Translator if (is_string($line)) { return $this->makeReplacements($line, $replace); } elseif (is_callable($line)) { - return app()->call($line, $replace); + return $this->makeReplacements( + app()->call($line, $replace), + $replace + ); } elseif (is_array($line) && count($line) > 0) { foreach ($line as $key => $value) { $line[$key] = $this->makeReplacements($value, $replace); diff --git a/tests/GlossTest.php b/tests/GlossTest.php index 4f4795d..d362b30 100644 --- a/tests/GlossTest.php +++ b/tests/GlossTest.php @@ -321,10 +321,13 @@ class GlossTest extends TestCase $this->addMessages('en', 'test', [ 'foo' => fn () => 'bar', 'abc' => fn ($resource, $title) => Str::upper($resource) . ' ' . Str::lower($title), + 'def' => fn ($resource) => "Edit {$resource} :title", ]); $this->assertSame('bar', gloss('test.foo')); $this->assertSame('PRODUCT macbook pro', gloss('test.abc', ['resource' => 'product', 'title' => 'MacBook Pro'])); + $this->assertSame('Edit product :title', gloss('test.def', ['resource' => 'product'])); + $this->assertSame('Edit product foo', gloss('test.def', ['resource' => 'product', 'title' => 'foo'])); } protected function addMessage(string $key, string $value, string $locale = 'en', string $group = 'test', string $namespace = null): void