F
Filamentβ€’8mo ago
el.polar

How can i translate the content of Forms\Components\Select?

I'm using a list at constants class. Any suggestion? The translateLabel() just translate the label πŸ€ͺ obviously
No description
No description
Solution:
It's work!!! but one question :c why i need add "case mother = 1;" ? what its mean?...
No description
Jump to solution
7 Replies
Povilas K
Povilas Kβ€’8mo ago
Can you maybe load the data into Select already translated, instead of loading them from the config?
el.polar
el.polarOPβ€’8mo ago
The goal is to use the list in case elements are increased in the future and use that constant list in several places. Or can you show me an example please? PS: Sorry if my English is not so good 😬
dissto
disstoβ€’8mo ago
Use an Enum πŸ˜‹ Here is an example from a current project: Maybe it helps you ☺️
<?php

namespace App\Enums;

use Filament\Support\Colors\Color;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;

enum ProjectStatus: int implements HasColor, HasIcon, HasLabel
{
case Active = 1;
case Planned = 2;
case Completed = 3;
case Cancelled = 4;
case OnHold = 5;
case BudgetNotMet = 6;

public function getColor(): string|array|null
{
return match ($this) {
self::Active => Color::Green,
...
};
}

public function getIcon(): ?string
{
return match ($this) {
self::Active => 'heroicon-o-check-circle',
...
};
}

public function getLabel(): ?string
{
return match ($this) {
self::Active => __('enums/project_status.labels.active'),
self::Planned => __('enums/project_status.labels.planned'),
self::Completed => __('enums/project_status.labels.completed'),
self::Cancelled => __('enums/project_status.labels.cancelled'),
self::OnHold => __('enums/project_status.labels.on_hold'),
self::BudgetNotMet => __('enums/project_status.labels.budget_not_met'),
};
}
}
<?php

namespace App\Enums;

use Filament\Support\Colors\Color;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;

enum ProjectStatus: int implements HasColor, HasIcon, HasLabel
{
case Active = 1;
case Planned = 2;
case Completed = 3;
case Cancelled = 4;
case OnHold = 5;
case BudgetNotMet = 6;

public function getColor(): string|array|null
{
return match ($this) {
self::Active => Color::Green,
...
};
}

public function getIcon(): ?string
{
return match ($this) {
self::Active => 'heroicon-o-check-circle',
...
};
}

public function getLabel(): ?string
{
return match ($this) {
self::Active => __('enums/project_status.labels.active'),
self::Planned => __('enums/project_status.labels.planned'),
self::Completed => __('enums/project_status.labels.completed'),
self::Cancelled => __('enums/project_status.labels.cancelled'),
self::OnHold => __('enums/project_status.labels.on_hold'),
self::BudgetNotMet => __('enums/project_status.labels.budget_not_met'),
};
}
}
And the translation (lang/en/enums/project_status.php)
<?php

return [
'labels' => [
'active' => 'Active',
'planned' => 'Planned',
'completed' => 'Completed',
'cancelled' => 'Cancelled',
'on_hold' => 'On hold',
'budget_not_met' => 'Budget not met',
],
];
<?php

return [
'labels' => [
'active' => 'Active',
'planned' => 'Planned',
'completed' => 'Completed',
'cancelled' => 'Cancelled',
'on_hold' => 'On hold',
'budget_not_met' => 'Budget not met',
],
];
Select::make('status')
->options(ProjectStatus::class)
Select::make('status')
->options(ProjectStatus::class)
Solution
el.polar
el.polarβ€’8mo ago
It's work!!! but one question :c why i need add "case mother = 1;" ? what its mean?
No description
dissto
disstoβ€’8mo ago
To have a value in your table. Right now since you set it to : int this would mean your relationship column is expected to be an int too, if thats what you mean? You can use string if thats better suited for you though. No harm in that
enum Relationship: string implements HasLabel
{
case Mother = 'mother';

...
enum Relationship: string implements HasLabel
{
case Mother = 'mother';

...
Dennis Koch
Dennis Kochβ€’8mo ago
Check the docs for BackedEnums if you want to learn more
el.polar
el.polarOPβ€’8mo ago
Thank U Team 🧐 i'm gonna read more about that
Want results from more Discord servers?
Add your server