Translating labels
readable() looks the headlined label up in Laravel’s translator, so the headlined string is the translation key. Add it to a JSON translation file such as lang/sk.json and every label helper follows the active locale:
{
"Hot News": "Horúce správy",
"Regular News": "Bežné správy",
"Private News": "Súkromné správy"
}app()->setLocale('sk');
NewsCategory::HotNews->label(); // "Horúce správy"
NewsCategory::labels(); // ['Horúce správy', 'Bežné správy', 'Súkromné správy']
NewsCategory::toOptions(); // ['hot-news' => 'Horúce správy', ...]
NewsCategory::fromLabel('Horúce správy'); // NewsCategory::HotNews
NewsCategory::fromLabel('Hot News'); // throws EnumException — the active label is SlovakfromLabel() and tryFromLabel() match against readable(), so they resolve the label in the current locale — handy for importing data typed by people who see the translated UI.
Custom labels
When a label can’t be derived from the value — int-backed enums, abbreviations, or wording that differs from the value — override readable() on the enum. Methods declared on the enum take precedence over the trait, and label(), labels(), toOptions(), options() and the label lookups all pick up your version:
use RoundlyConsulting\Enums\Helpers;
enum Priority: int
{
use Helpers;
case Low = 0;
case Medium = 5;
case High = 10;
public function readable(): string
{
return match ($this) {
self::Low => __('Low priority'),
self::Medium => __('Medium priority'),
self::High => __('High priority'),
};
}
}
Priority::Low->label(); // "Low priority"
Priority::toOptions(); // [0 => 'Low priority', 5 => 'Medium priority', 10 => 'High priority']
Priority::fromLabel('High priority'); // Priority::HighShow your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.