PHP code example of mohamedbakr57 / localized-enum

1. Go to this page and download the library: Download mohamedbakr57/localized-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mohamedbakr57 / localized-enum example snippets


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

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

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

enum TestStatus: string
{
    use HasLabel;

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

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