PHPackages                             osama-98/laravel-enum-translatable - 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. osama-98/laravel-enum-translatable

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

osama-98/laravel-enum-translatable
==================================

A Laravel package that provides translatable enum functionality with easy-to-use methods for working with enum values and their translations

2.2.1(1mo ago)561.2k—5.6%2MITPHPPHP ^8.2CI passing

Since Nov 25Pushed 1w agoCompare

[ Source](https://github.com/osama-98/laravel-enum-translatable)[ Packagist](https://packagist.org/packages/osama-98/laravel-enum-translatable)[ Docs](https://github.com/osama-98/laravel-enum-translatable)[ GitHub Sponsors](https://github.com/osama-98)[ RSS](/packages/osama-98-laravel-enum-translatable/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (52)Versions (13)Used By (0)

[![Laravel Enum Translatable](.github/logo.svg)](.github/logo.svg)

[![Latest Version on Packagist](https://camo.githubusercontent.com/aed1d09d81a90da425bdb1940c6c80288132eb96bf766fd8bfe7fc0acae97fb0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f73616d612d39382f6c61726176656c2d656e756d2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/osama-98/laravel-enum-translatable)[![Total Downloads](https://camo.githubusercontent.com/69d871da9a2d6fd0480ae890f2b89efcc6c891cc784985d2bbe2f6a307ce81b0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f73616d612d39382f6c61726176656c2d656e756d2d7472616e736c617461626c652e737667)](https://packagist.org/packages/osama-98/laravel-enum-translatable)[![License](https://camo.githubusercontent.com/58af982955983e1341e53a7169acd59c73b79dd10365f2c8ad2e11c30dde3f20/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f73616d612d39382f6c61726176656c2d656e756d2d7472616e736c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/osama-98/laravel-enum-translatable)

Laravel Enum Translatable
=========================

[](#laravel-enum-translatable)

A Laravel package that extends PHP 8.2 backed enums with first-class translation support, fluent array helpers, and safe comparison utilities — all through composable traits.

**References:** [Medium Article](https://masteryoflaravel.medium.com/stop-hardcoding-translations-the-revolutionary-way-to-build-multilingual-laravel-apps-with-bf303533b8b0) · [Laravel News](https://laravel-news.com/translatable-enums)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#references-medium-article--laravel-news)

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

[](#table-of-contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Configuration](#configuration)
- [Available Traits](#available-traits)
- [Generating Enums](#generating-enums)
- [Usage](#usage)
    - [Translation Key Convention](#translation-key-convention)
    - [EnumTranslatable](#enumtranslatable)
    - [EnumArrayable](#enumarrayable)
    - [EnumWrappable](#enumwrappable)
- [Real-World Examples](#real-world-examples)
- [Testing](#testing)
- [Changelog](#changelog)
- [Credits](#credits)
- [License](#license)

---

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Laravel 11.0 or higher

---

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

[](#installation)

Install the package via Composer:

```
composer require osama-98/laravel-enum-translatable
```

---

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

[](#quick-start)

**Step 1.** Generate an enum using the Artisan command:

```
php artisan make:enum OrderStatus
```

**Step 2.** Define your cases and apply the trait:

```
enum OrderStatusEnum: string
{
    use EnumTranslatable;

    case PENDING   = 'pending';
    case SHIPPED   = 'shipped';
    case DELIVERED = 'delivered';
}
```

**Step 3.** Add translation entries in `lang/en/enums.php`:

```
return [
    'order_statuses' => [
        'pending'   => 'Pending',
        'shipped'   => 'Shipped',
        'delivered' => 'Delivered',
    ],
];
```

**Step 4.** Use the enum in your application:

```
OrderStatusEnum::PENDING->trans();      // 'Pending'
OrderStatusEnum::PENDING->trans('ar');  // 'قيد الانتظار'
OrderStatusEnum::toArrayTrans();        // [['value' => 'pending', 'name' => 'Pending'], ...]
```

---

Configuration
-------------

[](#configuration)

Publish the configuration file to customise supported locales and modular support:

```
php artisan vendor:publish --tag="laravel-enums-config"
```

```
// config/laravel-enums.php

return [

    /*
    |--------------------------------------------------------------------------
    | Supported Locales
    |--------------------------------------------------------------------------
    | The locales that allTrans() will return translations for.
    */
    'supported_locales' => ['en'],

    /*
    |--------------------------------------------------------------------------
    | Modular Support
    |--------------------------------------------------------------------------
    | Enable this if you use a module system such as nWidart/laravel-modules.
    | Translations will be loaded from each module's namespace automatically.
    */
    'modular_enabled' => false,

    /*
    |--------------------------------------------------------------------------
    | Namespace Resolver
    |--------------------------------------------------------------------------
    | The class responsible for resolving a module's translation namespace.
    | Extend TranslationNamespaceResolver to customise the resolution logic.
    */
    'namespace_resolver' => \Osama\LaravelEnums\TranslationNamespaceResolver::class,

];
```

---

Available Traits
----------------

[](#available-traits)

The package provides three traits that compose on top of one another:

```
EnumTranslatable
└── EnumArrayable
    └── EnumWrappable

```

TraitIntended Use`EnumTranslatable`Enums that require translated labels. Includes all traits below.`EnumArrayable`Enums used for filtering or listing, without translation.`EnumWrappable`Enums that only need comparison and safe-casting helpers.---

Generating Enums
----------------

[](#generating-enums)

Use the `make:enum` Artisan command to scaffold a new enum class:

```
# String-backed with EnumTranslatable (default)
php artisan make:enum OrderStatus

# Integer-backed
php artisan make:enum OrderStatus --int

# With EnumArrayable
php artisan make:enum OrderStatus --arrayable

# With EnumWrappable
php artisan make:enum OrderStatus --wrappable
```

> **Note:** The `--arrayable` flag already includes `EnumWrappable` internally. There is no need to combine both flags.

Generated files are placed in `app/Enums/`. Nested namespaces are supported using `/`:

```
php artisan make:enum Admin/UserStatus
# Generates: app/Enums/Admin/UserStatusEnum.php
# Namespace:  App\Enums\Admin
```

The `Enum` suffix is appended automatically if it is not included in the provided name.

---

Usage
-----

[](#usage)

All examples in this section use the following enum definition:

```
