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.
Fund package maintenance!
Bakr
Requires
- php: ^8.1 || ^8.2 || ^8.3
- illuminate/contracts: ^10.0 || ^11.0 || ^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9 || ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1 || ^7.10.0
- orchestra/testbench: ^10.0 || ^11.0 || ^12.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3 || ^2.0
- phpstan/phpstan-deprecation-rules: ^1.1 || ^2.0
- phpstan/phpstan-phpunit: ^1.3 || ^2.0
This package is auto-updated.
Last update: 2025-07-06 13:50:31 UTC
README
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
π§ͺ Usage
Basic Usage
TestStatus::Approved->label(); // Output: "Approved by Admin" (if translation exists)
Custom Translation Key
TestStatus::Approved->label('custom.status.approved'); // Output: value from that specific key
Fallback Default
TestStatus::Approved->label('missing.key', 'Approved fallback'); // Output: "Approved fallback" if translation not found
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
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
- π·οΈ 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
composer require mohamedbakr57/localized-enum
π 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
- PHP: ^8.1
- Laravel: ^10.0, ^11.0, ^12.0
π§ͺ Testing
composer test
composer test-coverage
Run Pint for formatting:
composer format
π§° Development
git clone https://github.com/mohamedbakr57/localized-enum.git cd localized-enum composer install composer test
π License
Licensed under MIT License
π Credits
Built and maintained by Mohamed Bakr
Stars and PRs are welcome βοΈ