Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer collective, backed by the Pireon OAuth identity server. The public site is a separate, server-rendered application consuming /api/public/v1. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
59 lines
2.2 KiB
PHP
59 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Support;
|
|
|
|
use Closure;
|
|
use Filament\Schemas\Components\Tabs;
|
|
use Filament\Schemas\Components\Tabs\Tab;
|
|
|
|
/**
|
|
* Renders one tab per supported locale for a model's translatable fields.
|
|
*
|
|
* This leans on how spatie/laravel-translatable already behaves rather than
|
|
* adding a translation layer of its own: `toArray()` hands Filament the full
|
|
* locale => value map, and assigning that map back through `setAttribute`
|
|
* writes each translation. So a field whose state path is "title.ca" hydrates
|
|
* and saves the Catalan translation with no extra wiring.
|
|
*/
|
|
final class TranslatableTabs
|
|
{
|
|
/**
|
|
* @param Closure(string $locale, bool $isDefault): array<int, mixed> $fields
|
|
* Receives the locale code and whether it is the primary one,
|
|
* and returns the components for that tab.
|
|
*/
|
|
public static function make(Closure $fields, ?string $label = null): Tabs
|
|
{
|
|
$default = (string) config('locales.default');
|
|
|
|
$tabs = [];
|
|
|
|
foreach (config('locales.supported') as $locale => $meta) {
|
|
$tabs[] = Tab::make($locale)
|
|
->label(trim(($meta['flag'] ?? '').' '.($meta['native'] ?? $locale)))
|
|
// Marks the primary language, the one every other tab falls
|
|
// back to when left empty.
|
|
->badge($locale === $default ? '★' : null)
|
|
->schema($fields($locale, $locale === $default));
|
|
}
|
|
|
|
$tabs = Tabs::make($label ?? __('panel.pages.fields.translations'))
|
|
->tabs($tabs)
|
|
->columnSpanFull();
|
|
|
|
// Open the tab for the language the admin is currently working in.
|
|
return $tabs->persistTabInQueryString('locale_tab');
|
|
}
|
|
|
|
/**
|
|
* Suffix for a field label inside a locale tab, e.g. "Título (Català)".
|
|
* Useful when a field sits outside the tabs but still targets one locale.
|
|
*/
|
|
public static function suffix(string $locale): string
|
|
{
|
|
return (string) (config("locales.supported.{$locale}.native") ?? $locale);
|
|
}
|
|
}
|