PHPackages                             mohamedbakr57/localized-enum - 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. mohamedbakr57/localized-enum

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

mohamedbakr57/localized-enum
============================

A simple and lightweight Laravel package that adds localized labels to native PHP enums using Laravel's translation system. Ideal for multi-language applications.

v1.0.0(12mo ago)217[3 PRs](https://github.com/mohamedbakr57/laravel-localized-enum/pulls)MITPHPPHP ^8.1 || ^8.2 || ^8.3CI passing

Since Jul 5Pushed 2mo agoCompare

[ Source](https://github.com/mohamedbakr57/laravel-localized-enum)[ Packagist](https://packagist.org/packages/mohamedbakr57/localized-enum)[ Docs](https://github.com/mohamedbakr57/localized-enum)[ GitHub Sponsors](https://github.com/Bakr)[ RSS](/packages/mohamedbakr57-localized-enum/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (12)Versions (6)Used By (0)

Localized Enum for Laravel
==========================

[](#localized-enum-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5ddbee90b16e00f74dd6bce6568b3e9dcc07dfadd862a61b34da3a6f4e8707ba/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f68616d656462616b7235372f6c6f63616c697a65642d656e756d2e737667)](https://packagist.org/packages/mohamedbakr57/localized-enum)
[![Tests](https://github.com/mohamedbakr57/localized-enum/actions/workflows/run-tests.yml/badge.svg)](https://github.com/mohamedbakr57/localized-enum/actions)
[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](LICENSE)

**Localized Enum** is a simple, lightweight Laravel package that adds localized labels to native PHP enums using Laravel’s translation system.
Perfect for multilingual applications and API responses with dynamic localization support.

---

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

[](#-table-of-contents)

- [Usage](#-usage)
    - [Basic Usage](#basic-usage)
    - [Custom Translation Key](#custom-translation-key)
    - [Fallback Default](#fallback-default)
    - [Locale from Request Header](#locale-from-request-header)
    - [Override Header Key](#override-header-key)
- [Features](#-features)
- [Installation](#-installation)
- [Example Translation File](#-example-translation-file)
- [Testing](#-testing)
- [Development](#-development)
- [License](#-license)
- [Credits](#-credits)

---

🧪 Usage
-------

[](#-usage)

### Basic Usage

[](#basic-usage)

```
TestStatus::Approved->label();
// Output: "Approved by Admin" (if translation exists)
```

### Custom Translation Key

[](#custom-translation-key)

```
TestStatus::Approved->label('custom.status.approved');
// Output: value from that specific key
```

### Fallback Default

[](#fallback-default)

```
TestStatus::Approved->label('missing.key', 'Approved fallback');
// Output: "Approved fallback" if translation not found
```

### Locale from Request Header

[](#locale-from-request-header)

If you're building an API and send locale via headers:

```
GET /api/user
X-Locale: ar
```

The trait will use the `X-Locale` value automatically.

> 📝 Default header key is `X-Locale`, but it can be overridden.

### Override Header Key

[](#override-header-key)

If your app uses a different header, override the method in your enum:

```
enum TestStatus: string
{
    use HasLabel;

    protected function getLocaleHeaderKey(): string
    {
        return 'Accept-Language';
    }
}
```

Or override `getLabelLocale()` entirely for full control.

---

✨ Features
----------

[](#-features)

- 🏷️ Adds `label()` method to native PHP Enums
- 🌐 Fully supports Laravel’s translation system
- 🧠 Smart fallback resolution (from multiple key patterns)
- 🧪 Works great in API responses
- 🔧 Easily override locale detection via request headers
- 🔄 Defaults to `config('app.locale')` if no locale is sent
- ⚡ Compatible with flat or nested translation files

---

📦 Installation
--------------

[](#-installation)

```
composer require mohamedbakr57/localized-enum
```

---

📌 Example Translation File
--------------------------

[](#-example-translation-file)

```
// lang/en/enums.php
return [
    'TestStatus.Approved' => 'Approved by Admin',
    'TestStatus.Pending'  => 'Waiting',
    'TestStatus.Rejected' => 'Rejected',
];
```

Supports both:

- `enums.FQCN.CASE`
- `enums.Basename.CASE`
- `FQCN.CASE`
- `Basename.CASE`
- Or just: `'Approved' => 'Approved Label'` for flat key fallback

---

✅ Requirements
--------------

[](#-requirements)

- PHP: ^8.1
- Laravel: ^10.0, ^11.0, ^12.0

---

🧪 Testing
---------

[](#-testing)

```
composer test
composer test-coverage
```

Run Pint for formatting:

```
composer format
```

---

🧰 Development
-------------

[](#-development)

```
git clone https://github.com/mohamedbakr57/localized-enum.git
cd localized-enum
composer install
composer test
```

---

📄 License
---------

[](#-license)

Licensed under [MIT License](LICENSE)

---

🙌 Credits
---------

[](#-credits)

Built and maintained by [Mohamed Bakr](https://github.com/mohamedbakr57)
Stars and PRs are welcome ⭐️

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance70

Regular maintenance activity

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

364d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/61648550?v=4)[Mohamed Bakr](/maintainers/mohamedbakr57)[@mohamedbakr57](https://github.com/mohamedbakr57)

---

Top Contributors

[![mohamedbakr57](https://avatars.githubusercontent.com/u/61648550?v=4)](https://github.com/mohamedbakr57 "mohamedbakr57 (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laravelBakrlocalized-enum

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/mohamedbakr57-localized-enum/health.svg)

```
[![Health](https://phpackages.com/badges/mohamedbakr57-localized-enum/health.svg)](https://phpackages.com/packages/mohamedbakr57-localized-enum)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k102.4M1.4k](/packages/spatie-laravel-permission)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.1k11.2M100](/packages/dedoc-scramble)[spatie/laravel-passkeys

Use passkeys in your Laravel app

471890.7k39](/packages/spatie-laravel-passkeys)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[elegantly/laravel-translator

All on one translations management for Laravel

6333.1k](/packages/elegantly-laravel-translator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
