ezappslab/filament-translatable 问题修复 & 功能扩展

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

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

ezappslab/filament-translatable

Composer 安装命令:

composer require ezappslab/filament-translatable

包简介

Filament Translatable adds locale-aware resource pages to FilamentPHP panels using Spatie Laravel Translatable

README 文档

README

Filament Translatable adds locale-aware resource pages for Filament panels that use spatie/laravel-translatable.

The package provides:

  • A Filament panel plugin for configuring available locales.
  • Page concerns for create, edit, list, and view resource pages.
  • A locale selector header action.
  • A content driver that reads, writes, displays, and searches the active locale of Spatie translatable attributes.

Requirements

  • PHP 8.2 or higher
  • Filament 5
  • spatie/laravel-translatable 6.11.4 or higher

Installation

Install the package with Composer:

composer require ezappslab/filament-translatable

Run the install command:

php artisan filament-translatable:install

The installer can publish the configuration file and register the package service provider in your application.

Configuration

Configure the locales that should be available in your Filament panel:

use Infinity\FilamentTranslatable\Enums\Locale;

return [
    'locales' => [
        Locale::English,
        Locale::German,
        Locale::Spanish,
    ],

    'fallback_locale' => Locale::English,
];

You can also configure locales directly when registering the plugin on a panel:

use Filament\Panel;
use Filament\PanelProvider;
use Infinity\FilamentTranslatable\Enums\Locale;
use Infinity\FilamentTranslatable\FilamentTranslatablePlugin;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->path('admin')
            ->plugin(
                FilamentTranslatablePlugin::make()
                    ->locales([
                        Locale::English,
                        Locale::German,
                    ])
                    ->fallbackLocale(Locale::English)
            );
    }
}

Preparing Models

Use Spatie's HasTranslations trait on any model that has translated attributes. The translated columns should be JSON-compatible columns in your database.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class Product extends Model
{
    use HasTranslations;

    public array $translatable = [
        'name',
        'description',
    ];

    protected $fillable = [
        'name',
        'description',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'name' => 'array',
            'description' => 'array',
            'is_active' => 'boolean',
        ];
    }
}

Example migration columns:

$table->json('name');
$table->json('description');
$table->boolean('is_active')->default(true);

Using Translatable Resource Pages

Add the matching concern to each Filament resource page and add the locale selector to the page header actions.

List Page

namespace App\Filament\Resources\ProductResource\Pages;

use App\Filament\Resources\ProductResource;
use Filament\Resources\Pages\ListRecords;
use Infinity\FilamentTranslatable\Actions\SelectLocaleAction;
use Infinity\FilamentTranslatable\Resources\Pages\Concerns\HasTranslatableListRecords;

class ListProducts extends ListRecords
{
    use HasTranslatableListRecords;

    protected static string $resource = ProductResource::class;

    protected function getHeaderActions(): array
    {
        return [
            SelectLocaleAction::make(),
        ];
    }
}

Create Page

namespace App\Filament\Resources\ProductResource\Pages;

use App\Filament\Resources\ProductResource;
use Filament\Resources\Pages\CreateRecord;
use Infinity\FilamentTranslatable\Actions\SelectLocaleAction;
use Infinity\FilamentTranslatable\Resources\Pages\Concerns\HasTranslatableCreateRecord;

class CreateProduct extends CreateRecord
{
    use HasTranslatableCreateRecord;

    protected static string $resource = ProductResource::class;

    protected function getHeaderActions(): array
    {
        return [
            SelectLocaleAction::make(),
        ];
    }
}

Edit Page

namespace App\Filament\Resources\ProductResource\Pages;

use App\Filament\Resources\ProductResource;
use Filament\Resources\Pages\EditRecord;
use Infinity\FilamentTranslatable\Actions\SelectLocaleAction;
use Infinity\FilamentTranslatable\Resources\Pages\Concerns\HasTranslatableEditRecord;

class EditProduct extends EditRecord
{
    use HasTranslatableEditRecord;

    protected static string $resource = ProductResource::class;

    protected function getHeaderActions(): array
    {
        return [
            SelectLocaleAction::make(),
        ];
    }
}

View Page

namespace App\Filament\Resources\ProductResource\Pages;

use App\Filament\Resources\ProductResource;
use Filament\Resources\Pages\ViewRecord;
use Infinity\FilamentTranslatable\Actions\SelectLocaleAction;
use Infinity\FilamentTranslatable\Resources\Pages\Concerns\HasTranslatableViewRecord;

class ViewProduct extends ViewRecord
{
    use HasTranslatableViewRecord;

    protected static string $resource = ProductResource::class;

    protected function getHeaderActions(): array
    {
        return [
            SelectLocaleAction::make(),
        ];
    }
}

Resource Example

Once the page concerns are installed, build your resource form and table normally. The package resolves translatable fields through the currently selected locale.

namespace App\Filament\Resources;

use App\Filament\Resources\ProductResource\Pages;
use App\Models\Product;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ProductResource extends Resource
{
    protected static ?string $model = Product::class;

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            TextInput::make('name')
                ->required(),
            Textarea::make('description')
                ->required(),
            Toggle::make('is_active'),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table->columns([
            TextColumn::make('name')
                ->searchable(),
            TextColumn::make('description'),
            IconColumn::make('is_active')
                ->boolean(),
        ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListProducts::route('/'),
            'create' => Pages\CreateProduct::route('/create'),
            'view' => Pages\ViewProduct::route('/{record}'),
            'edit' => Pages\EditProduct::route('/{record}/edit'),
        ];
    }
}

When a user selects Bulgarian, name and description are read from and written to the bg translation values. Non-translatable attributes, such as is_active, are handled as normal model attributes.

Relationship Fields

Translatable fields inside Filament relationship sections are also supported. Mark the related model attributes as translatable and use the same page concerns on the parent resource pages.

use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

public static function form(Schema $schema): Schema
{
    return $schema->components([
        TextInput::make('name')
            ->required(),
        TextInput::make('email')
            ->email()
            ->required(),
        Section::make('Profile')
            ->relationship('profile')
            ->schema([
                TextInput::make('headline')
                    ->required(),
                TextInput::make('biography')
                    ->required(),
                Toggle::make('is_public'),
            ]),
    ]);
}

In this example, profile.headline and profile.biography can be translated per locale when the Profile model uses Spatie's HasTranslations trait.

Relation Managers

Use HasTranslatableRelationManager on Filament relation managers that manage models with Spatie translatable attributes. Add SelectLocaleAction to the table header actions so users can switch the active locale.

namespace App\Filament\Resources\UserResource\RelationManagers;

use Filament\Actions\CreateAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Infinity\FilamentTranslatable\Actions\SelectLocaleAction;
use Infinity\FilamentTranslatable\Resources\RelationManagers\Concerns\HasTranslatableRelationManager;

class ProductsRelationManager extends RelationManager
{
    use HasTranslatableRelationManager;

    protected static string $relationship = 'products';

    public function form(Schema $schema): Schema
    {
        return $schema->components([
            TextInput::make('name')
                ->required(),
            Textarea::make('description')
                ->required(),
        ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->searchable(),
                TextColumn::make('description'),
            ])
            ->headerActions([
                SelectLocaleAction::make(),
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
            ]);
    }
}

The relation manager uses its own active locale session key, scoped by panel, page, and relation manager class.

Scripts

  • composer lint: Run Pint and PHPStan.
  • composer test: Run Pest tests.
  • composer build: Build workbench assets.
  • composer serve: Serve the workbench application.

Documentation

For more detailed information about the included tools, see docs/tooling.md.

ezappslab/filament-translatable 适用场景与选型建议

ezappslab/filament-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ezappslab/filament-translatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ezappslab/filament-translatable 我们能提供哪些服务?
定制开发 / 二次开发

基于 ezappslab/filament-translatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-27