Skip to content

Commit ef5aa3d

Browse files
authored
[12.x] Add support for Enums in Translator replacements (#58048)
1 parent 73fe9d3 commit ef5aa3d

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

src/Illuminate/Translation/Translator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Illuminate\Support\Traits\ReflectsClosures;
1313
use InvalidArgumentException;
1414

15+
use function Illuminate\Support\enum_value;
16+
1517
class Translator extends NamespacedItemResolver implements TranslatorContract
1618
{
1719
use Macroable, ReflectsClosures;
@@ -285,8 +287,10 @@ protected function makeReplacements($line, array $replace)
285287
continue;
286288
}
287289

288-
if (is_object($value) && isset($this->stringableHandlers[get_class($value)])) {
289-
$value = call_user_func($this->stringableHandlers[get_class($value)], $value);
290+
if (is_object($value)) {
291+
$value = isset($this->stringableHandlers[get_class($value)])
292+
? call_user_func($this->stringableHandlers[get_class($value)], $value)
293+
: enum_value($value);
290294
}
291295

292296
$shouldReplace[':'.Str::ucfirst($key)] = Str::ucfirst($value ?? '');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Translation\Fixtures\Enums;
4+
5+
enum Bar: int
6+
{
7+
case Thirteen = 13;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Translation\Fixtures\Enums;
4+
5+
enum Baz: string
6+
{
7+
case February = 'February';
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Translation\Fixtures\Enums;
4+
5+
enum Foo
6+
{
7+
case Hosni;
8+
}

tests/Translation/TranslationTranslatorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Illuminate\Contracts\Translation\Loader;
66
use Illuminate\Support\Carbon;
77
use Illuminate\Support\Collection;
8+
use Illuminate\Tests\Translation\Fixtures\Enums\Bar;
9+
use Illuminate\Tests\Translation\Fixtures\Enums\Baz;
10+
use Illuminate\Tests\Translation\Fixtures\Enums\Foo;
811
use Illuminate\Translation\MessageSelector;
912
use Illuminate\Translation\Translator;
1013
use Mockery as m;
@@ -283,6 +286,35 @@ public function testGetJsonReplacesWithStringable()
283286
);
284287
}
285288

289+
public function testGetJsonReplacesWithEnums()
290+
{
291+
$t = new Translator($this->getLoader(), 'en');
292+
$t->getLoader()
293+
->shouldReceive('load')
294+
->once()
295+
->with('en', '*', '*')
296+
->andReturn([
297+
'string_backed_enum' => 'Laravel 12 was released in :month 2025',
298+
'int_backed_enum' => 'Stay tuned for Laravel v:version',
299+
'unit_enum' => ':person gets excited about every new Laravel release',
300+
]);
301+
302+
$this->assertSame(
303+
'Laravel 12 was released in February 2025',
304+
$t->get('string_backed_enum', ['month' => Baz::February])
305+
);
306+
307+
$this->assertSame(
308+
'Stay tuned for Laravel v13',
309+
$t->get('int_backed_enum', ['version' => Bar::Thirteen])
310+
);
311+
312+
$this->assertSame(
313+
'Hosni gets excited about every new Laravel release',
314+
$t->get('unit_enum', ['person' => Foo::Hosni])
315+
);
316+
}
317+
286318
public function testTagReplacements()
287319
{
288320
$t = new Translator($this->getLoader(), 'en');

0 commit comments

Comments
 (0)