here are a few examples of using enums with localization in Laravel:
1. Using the BenSampo/laravel-enum Package:
The BenSampo/laravel-enum package provides built-in support for localizing enum descriptions. You can define your enum and implement the `LocalizedEnum` interface:
php
namespace App\Enums;
use BenSampo\Enum\Enum;
use BenSampo\Enum\Contracts\LocalizedEnum;
final class UserRole extends Enum implements LocalizedEnum
{
const Administrator = 0;
const Moderator = 1;
const Subscriber = 2;
}
Then, add the translations to your language files (e.g., `resources/lang/en/enums.php`):
php
return [
UserRole::class => [
UserRole::Administrator => 'Administrator',
UserRole::Moderator => 'Moderator',
UserRole::Subscriber => 'Subscriber',
],
];
Now you can use the `description` property to get the localized value:
php
$role = UserRole::Moderator;
echo $role->description; // Output: "Moderator"
2. Using a Custom Trait:
If you're not using the BenSampo package, you can create a custom trait to handle localization of enum values:
php
namespace App\Enums\Concerns;
trait Translatable
{
public function getLabel(): string
{
$key = sprintf('%s.%s', static::class, $this->value);
return __($key);
}
}
Then, in your enum class, use the `Translatable` trait:
php
namespace App\Enums;
use App\Enums\Concerns\Translatable;
enum UserRole: string
{
use Translatable;
case Administrator = 'administrator';
case Moderator = 'moderator';
case Subscriber = 'subscriber';
}
And add the translations to your language files (e.g., `resources/lang/en/enums.php`):
php
return [
'App\Enums\UserRole' => [
'administrator' => 'Administrator',
'moderator' => 'Moderator',
'subscriber' => 'Subscriber',
],
];
Now you can use the `getLabel()` method to get the localized value:
php
$role = UserRole::Moderator;
echo $role->getLabel(); // Output: "Moderator"
3. Using a Custom Enum Class:
Another approach is to create a custom Enum class that handles localization:
php
namespace App\Enums;
use BenSampo\Enum\Enum;
use Illuminate\Support\Facades\Lang;
abstract class LocalizedEnum extends Enum
{
public function getDescription(): string
{
$key = $this->getTranslationKey();
return Lang::get($key);
}
protected function getTranslationKey(): string
{
return sprintf('enums.%s.%s', static::class, $this->value);
}
}
// Usage
final class UserRole extends LocalizedEnum
{
const Administrator = 'administrator';
const Moderator = 'moderator';
const Subscriber = 'subscriber';
}
And add the translations to your language files (e.g., `resources/lang/en/enums.php`):
php
return [
'App\Enums\UserRole' => [
'administrator' => 'Administrator',
'moderator' => 'Moderator',
'subscriber' => 'Subscriber',
],
];
Now you can use the `getDescription()` method to get the localized value:
php
$role = UserRole::Moderator;
echo $role->getDescription(); // Output: "Moderator"
These examples demonstrate how you can integrate enum localization in your Laravel applications using different approaches. The choice of which one to use will depend on your specific requirements and coding style.
Citations:[1] https://www.youtube.com/watch?v=g9xNNbkwg_E
[2] https://github.com/BenSampo/laravel-enum/issues/206
[3] https://stackoverflow.com/questions/76281835/how-to-use-laravel-enum-by-extra-attributes-and-localization
[4] https://abunooh.com/how-to-add-translatable-enum-options-in-laravel
[5] https://laracasts.com/discuss/channels/laravel/localize-enum-or-set-data-type-on-laravel