1
0
Fork 0
mirror of https://github.com/archtechx/gloss.git synced 2025-12-12 03:04:04 +00:00

Improve callable strings

This commit is contained in:
Samuel Štancl 2021-03-16 21:14:39 +01:00
parent 1beb64a7bb
commit b7df00dd1f
2 changed files with 7 additions and 1 deletions

View file

@ -228,7 +228,10 @@ class GlossTranslator extends Translator
if (is_string($line)) { if (is_string($line)) {
return $this->makeReplacements($line, $replace); return $this->makeReplacements($line, $replace);
} elseif (is_callable($line)) { } elseif (is_callable($line)) {
return app()->call($line, $replace); return $this->makeReplacements(
app()->call($line, $replace),
$replace
);
} elseif (is_array($line) && count($line) > 0) { } elseif (is_array($line) && count($line) > 0) {
foreach ($line as $key => $value) { foreach ($line as $key => $value) {
$line[$key] = $this->makeReplacements($value, $replace); $line[$key] = $this->makeReplacements($value, $replace);

View file

@ -321,10 +321,13 @@ class GlossTest extends TestCase
$this->addMessages('en', 'test', [ $this->addMessages('en', 'test', [
'foo' => fn () => 'bar', 'foo' => fn () => 'bar',
'abc' => fn ($resource, $title) => Str::upper($resource) . ' ' . Str::lower($title), 'abc' => fn ($resource, $title) => Str::upper($resource) . ' ' . Str::lower($title),
'def' => fn ($resource) => "Edit {$resource} :title",
]); ]);
$this->assertSame('bar', gloss('test.foo')); $this->assertSame('bar', gloss('test.foo'));
$this->assertSame('PRODUCT macbook pro', gloss('test.abc', ['resource' => 'product', 'title' => 'MacBook Pro'])); $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 protected function addMessage(string $key, string $value, string $locale = 'en', string $group = 'test', string $namespace = null): void