Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use Laravel's built-in localization features with enums


Can I use Laravel's built-in localization features with enums


Yes, you can use Laravel's built-in localization features with enums. Here are a few ways to do it:

1. Using the BenSampo/laravel-enum Package:

The BenSampo/laravel-enum package supports localization of enum descriptions out of the box. 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"

The key benefits of these approaches are:

1. Consistency: Providing a consistent way to localize enum values across your application.
2. Flexibility: Allowing you to customize the localization logic to fit your specific needs.
3. Maintainability: Centralizing the localization logic in one place, making it easier to update and manage.

Choose the approach that best fits your project's requirements and coding style.

Citations:
[1] https://laracasts.com/discuss/channels/laravel/localize-enum-or-set-data-type-on-laravel
[2] https://github.com/BenSampo/laravel-enum/issues/206
[3] https://packagist.org/packages/sourceboat/laravel-enumeration
[4] https://stackoverflow.com/questions/76281835/how-to-use-laravel-enum-by-extra-attributes-and-localization
[5] https://abunooh.com/how-to-add-translatable-enum-options-in-laravel