Looking to hire Laravel developers? Try LaraJobs

laravel-enum-translator maintained by joseftraxler

Description
Laravel extension for translating native php enum values.
Author
Josef Traxler
Last update
2026/04/18 11:25 (dev-main)
License
Links
Downloads
2

Comments
comments powered by Disqus

Laravel Enum Translator

Latest Version on Packagist Total Downloads PHP Version Laravel Version License

Lightweight Laravel helper for translating native PHP enums using attributes, Laravel’s translator, or automatic human‑readable fallback.

This package provides a simple trait you can add to any enum to get consistent, flexible translations without boilerplate.


Installation

composer require joseftraxler/laravel-enum-translator

Requirements:

  • PHP 8.4+
  • Laravel 12+

No service providers, no configuration files — works out of the box.


Usage

1) Add the trait to your enum

use JosefTraxler\LaravelEnumTranslator\TranslatableEnum;
use JosefTraxler\LaravelEnumTranslator\Attributes\Trans;

enum MyEnum: string
{
    use TranslatableEnum;

    #[Trans('My translated value')]
    case MyValue = 'my_value';
}

Get translated value

echo MyEnum::MyValue->trans();
// "My translated value"

Translation Sources (Priority Order)

The trait resolves translations in this order:

  1. Attribute translation (#[Trans('…')])
  2. Laravel translator (lang/en/enum.php)
  3. Human‑readable fallback

1) Attribute Translation

#[Trans('My translated value')]
case MyValue = 'my_value';

This is the highest‑priority translation.


2) Laravel Translator

You can define translations using Laravel's standard language files.

lang/en/enum.php

<?php

return [
    MyEnum::class => [
        MyEnum::MyValue->value => 'My translated value',
    ],
];

The translation key is automatically generated as:

enum.{EnumClass}.{value or name}

depending on whether the enum is backed.


3) Human‑Readable Fallback

If no attribute and no translation exists, the trait converts:

  • my_valueMy value
  • MyValueMy value
  • MY_VALUEMY VALUE (keeps acronyms)
  • my-valueMy value

Example:

enum Status: string {
    use TranslatableEnum;

    case InProgress = 'in_progress';
}

echo Status::InProgress->trans();
// "In progress"

Select Options Helper

Useful for forms (Filament, Laravel Form Components, Nova, etc.):

MyEnum::selectOptions();

Returns:

[
    'my_value' => 'My translated value',
]

HTML Output

The trait provides a toHtml() method that is fully compatible with Laravel’s Htmlable interface. The trait itself does not implement Htmlable, but if your enum implements it, Laravel will automatically call toHtml() when the enum instance is rendered in Blade.

This allows you to use the enum directly in templates without calling ->trans() manually.

Example enum definition:

use Illuminate\Contracts\Support\Htmlable;
use JosefTraxler\LaravelEnumTranslator\TranslatableEnum;

enum MyEnum: string implements Htmlable
{
    use TranslatableEnum;

    case MyValue = 'my_value';
}

Usage in Blade:

{{ MyEnum::MyValue }}

Laravel will internally call:

MyEnum::MyValue->toHtml();

Which is equivalent to:

MyEnum::MyValue->trans();

How It Works

The trait internally:

  • detects whether the enum is backed (value) or pure (name)
  • resolves translations via attribute → translator → fallback
  • uses Laravel’s translator service via App::make('translator')
  • formats fallback values using:
    • underscore/dash replacement
    • camelCase splitting
    • multibyte‑safe ucfirst

License

MIT © Josef Traxler