Laravel Packages

Translatable Enums

Published Updated
Translatable Enums image

Enums are a great addition to PHP and Laravel. They are very helpful with code maintainability and clarity. If you are building multilingual applications, you may need to translate those Enum values as well. This is where the Laravel Enum Translatable package, created by Osama Sadah, comes in.

Main Features

  • Translation Support: Automatically translate enum values using Laravel's translation system
  • Array Conversion: Convert enums to arrays with value and name for easy API responses
  • Object Method: Get enum as an object with value and translated name
  • Multiple Locales: Support for multiple locales with the trans() and allTrans() methods
  • Easy Integration: Simple trait-based implementation
  • Modular Support: Optional support for modular Laravel applications

Example

Ensure you add the EnumTranslatable trait to your Enum class.

namespace App\Enums\Article;

use Osama\LaravelEnums\Concerns\EnumTranslatable;

enum ArticleStatus: string
{
    use EnumTranslatable;

    case DRAFT = 'draft';
    case PUBLISHED = 'published';
    case REJECTED = 'rejected';
}

Now you can create translation files in the lang directory of your Laravel application. The translation key is automatically generated based on the enum class name.

For example, in lang/en/enums.php, you could have:

return [
    'article_statuses' => [
        'draft' => 'Draft',
        'published' => 'Published',
        'rejected' => 'Rejected',
    ],
];

And in lang/es/enums.php, you could have:

return [
    'article_statuses' => [
        'draft' => 'Borrador',
        'published' => 'Publicado',
        'rejected' => 'Rechazado',
    ],
];

To get the translated value of an Enum, use the trans() or transAll() method:

$status = ArticleStatus::DRAFT;

// Get translation in current locale
$translated = $status->trans();

// Get translation in specific locale
$english = $status->trans('en'); // Outputs: Draft
$spanish = $status->trans('es'); // Outputs: Borrador

// or All translations
$allTranslations = $status->allTrans(); // Outputs: ['en' => 'Draft', 'es' => 'Borrador']

The package also allows you to automatically cast translatable enums in your Eloquent models:

namespace App\Models;

use App\Enums\Article\ArticleStatus;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //...

    protected function casts(): array
    {
        return [
            'status' => ArticleStatus::class,
        ];
    }
}

Learn more about this package and view the source code on GitHub.

Also check out

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article
Insert Middleware Before or After Existing Laravel Middleware image

Insert Middleware Before or After Existing Laravel Middleware

Read article
Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys image

Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys

Read article
Strict Request Input Casting in Laravel image

Strict Request Input Casting in Laravel

Read article
Prevent Duplicate File Uploads With Laravel Storage Dedupler image

Prevent Duplicate File Uploads With Laravel Storage Dedupler

Read article
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article