mohamedbakr57/localized-enum 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mohamedbakr57/localized-enum

最新稳定版本:v1.0.0

Composer 安装命令:

composer require 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.

README 文档

README

Latest Version on Packagist
Tests
License: MIT

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 ⭐️

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固