PHPackages                             joseftraxler/laravel-enum-translator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. joseftraxler/laravel-enum-translator

ActiveLibrary[Localization &amp; i18n](/categories/localization)

joseftraxler/laravel-enum-translator
====================================

Lightweight Laravel helper for translating native PHP enums using attributes, Laravel translator, or human-readable fallback.

v1.2.0(4w ago)014MITPHPPHP &gt;=8.4CI passing

Since Apr 18Pushed 4w agoCompare

[ Source](https://github.com/joseftraxler/laravel-enum-translator)[ Packagist](https://packagist.org/packages/joseftraxler/laravel-enum-translator)[ Docs](https://github.com/JosefTraxler/laravel-enum-translator)[ RSS](/packages/joseftraxler-laravel-enum-translator/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (6)Dependencies (5)Versions (8)Used By (0)

Laravel Enum Translator
=======================

[](#laravel-enum-translator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/abc43cd135725bb1e6e6d767dece409666acfcbfae6aa236dece1e73b5999c1f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f736566747261786c65722f6c61726176656c2d656e756d2d7472616e736c61746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/joseftraxler/laravel-enum-translator)[![Total Downloads](https://camo.githubusercontent.com/a3caaf6951b7ae278234daa8b05060f64d38f490ba37c63dd1a7f1a1ea237d08/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f736566747261786c65722f6c61726176656c2d656e756d2d7472616e736c61746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/joseftraxler/laravel-enum-translator)[![PHP Version](https://camo.githubusercontent.com/4fc9b2b2def3086c8c17ad0da3a54ef454a9448d6ed816da7837f1bfb5212a73/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342532422d3737374242343f7374796c653d666c61742d737175617265266c6f676f3d706870)](https://camo.githubusercontent.com/4fc9b2b2def3086c8c17ad0da3a54ef454a9448d6ed816da7837f1bfb5212a73/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342532422d3737374242343f7374796c653d666c61742d737175617265266c6f676f3d706870)[![Laravel Version](https://camo.githubusercontent.com/1935ba84f80d8fd0428eeb3f169ff5e17470fcee31ea645e9eceff09797937be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322532422d4646324432303f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c)](https://camo.githubusercontent.com/1935ba84f80d8fd0428eeb3f169ff5e17470fcee31ea645e9eceff09797937be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322532422d4646324432303f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c)[![License](https://camo.githubusercontent.com/6c711032aff1ca0eb6b211aa6cb3649ce7fd64a7714e1181d4bb457f9680e7cf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/6c711032aff1ca0eb6b211aa6cb3649ce7fd64a7714e1181d4bb457f9680e7cf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666c61742d737175617265)[![CI](https://github.com/joseftraxler/laravel-enum-translator/actions/workflows/CI.yml/badge.svg)](https://github.com/joseftraxler/laravel-enum-translator/actions/workflows/CI.yml)

A lightweight, zero-configuration Laravel package that provides simple and elegant translation for native PHP enums using **attributes**, **Laravel's translator**, or automatic **human-readable fallback**.

This package eliminates the need for boilerplate code when working with enums that need translations. Just add the trait and start translating!

---

Table of Contents
-----------------

[](#table-of-contents)

- [Quick Start](#quick-start)
- [Installation](#installation)
- [Usage](#usage)
- [Translation Sources (Priority Order)](#translation-sources-priority-order)
- [Advanced Usage](#advanced-usage)
- [Real-World Examples](#real-world-examples)
- [Testing](#testing)
- [How It Works](#how-it-works)
- [Troubleshooting](#troubleshooting)
- [License](#license)

---

Quick Start
-----------

[](#quick-start)

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

enum Status: string
{
    use TranslatableEnum;

    #[Trans('In Progress')]
    case InProgress = 'in_progress';

    #[Trans('Completed')]
    case Completed = 'completed';
}

// Use it anywhere
echo Status::InProgress->trans(); // "In Progress"
```

---

Installation
------------

[](#installation)

```
composer require joseftraxler/laravel-enum-translator
```

### Requirements

[](#requirements)

- **PHP** 8.4+
- **Laravel** 12+

### Zero Configuration

[](#zero-configuration)

The package is **auto-discovered** by Laravel and requires **no configuration files or service providers**. It works out of the box!

---

Usage
-----

[](#usage)

### Step 1: Add the Trait to Your Enum

[](#step-1-add-the-trait-to-your-enum)

```
use JosefTraxler\LaravelEnumTranslator\TranslatableEnum;

enum MyEnum: string
{
    use TranslatableEnum;

    case MyValue = 'my_value';
}
```

### Step 2: Translate Enum Values

[](#step-2-translate-enum-values)

```
// Using the trait method
echo MyEnum::MyValue->trans();

// In Blade templates (if enum implements Htmlable)
{{ MyEnum::MyValue }}

// Get all enum options for forms
$options = MyEnum::selectOptions(); // ['my_value' => 'My value']
```

---

Translation Sources (Priority Order)
------------------------------------

[](#translation-sources-priority-order)

The package attempts to resolve translations in this order:

1. **Attribute Translation** (`#[Trans('…')]`) — Highest priority
2. **Laravel Translator** (language files)
3. **Human-Readable Fallback** — Always available

---

Advanced Usage
--------------

[](#advanced-usage)

### 1) Attribute Translation (Highest Priority)

[](#1-attribute-translation-highest-priority)

Use the `#[Trans()]` attribute to define hardcoded translations:

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

enum Priority: string
{
    use TranslatableEnum;

    #[Trans('Urgent')]
    case High = 'high';

    #[Trans('Normal')]
    case Medium = 'medium';

    #[Trans('Low Priority')]
    case Low = 'low';
}

echo Priority::High->trans(); // "Urgent"
```

**When to use:** Great for static translations that never change or when you want consistency without touching language files.

---

### 2) Laravel Translator (Flexible &amp; Multi-Language)

[](#2-laravel-translator-flexible--multi-language)

Use Laravel's standard language files for translations:

#### Create Language Files

[](#create-language-files)

**`resources/lang/en/enums.php`**

```
