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(10mo ago)27[3 PRs](https://github.com/mohamedbakr57/laravel-localized-enum/pulls)MITPHPPHP ^8.1 || ^8.2 || ^8.3CI passing

Since Jul 5Pushed 1mo 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 1mo ago

READMEChangelogDependencies (12)Versions (4)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

40

—

FairBetter than 87% of packages

Maintenance81

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity53

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

308d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9b5c1a4effdff5d824e0ba2e6b5679f129f054add69b91760f8e3a9e2ccbb616?d=identicon)[mohamedbakr57](/maintainers/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

[askdkc/breezejp

Laravel Starter Kit (Livewire+Breeze+Laravel UI+Jetstream)や標準のバリデーションメッセージを全て一瞬で日本語化し、言語切替機能も提供するパッケージです / This package provides all-in-one Japanese translation for Laravel StarterKit (Livewire StarterKit, Breeze, Laravel UI and Jetstream) packages and validation messages with language switching feature.

590244.8k1](/packages/askdkc-breezejp)[statikbe/laravel-filament-chained-translation-manager

A translation manager tool for Laravel Filament, that makes use of the Laravel Chained Translator.

92108.7k](/packages/statikbe-laravel-filament-chained-translation-manager)[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

24149.7k](/packages/vormkracht10-laravel-mails)[elegantly/laravel-translator

All on one translations management for Laravel

6216.9k](/packages/elegantly-laravel-translator)[andrewdwallo/transmatic

Automate and streamline real-time text translations in your Laravel applications

5511.6k](/packages/andrewdwallo-transmatic)[smousss/laravel-globalize

Make Laravel projects translatable in a matter of seconds!

2266.3k](/packages/smousss-laravel-globalize)

PHPackages © 2026

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