定制 go2stone/filament-translatable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

go2stone/filament-translatable

Composer 安装命令:

composer require go2stone/filament-translatable

包简介

This package adds a way to interact with go2stone/translatable in Filament.

README 文档

README

Filament Translatable

Filament Translatable

This package cloned from outerweb-filament-translatable-fields

We are using this for our use cases.

This package adds a way to make all filament fields translatable. It uses the spatie/laravel-translatable package in the background.

Installation

First install and configure your model(s) to use the spatie/laravel-translatable package.

You can install the package via composer:

composer require go2stone/filament-translatable

Add the plugin to your desired Filament panel:

use Go2Stone\FilamentTranslatable\Filament\Plugins\FilamentTranslatablePlugin;

class FilamentPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            // ...
            ->plugins([
                FilamentTranslatablePlugin::make(),
            ]);
    }
}

You can specify the supported locales:

use Go2Stone\FilamentTranslatable\Filament\Plugins\FilamentTranslatablePlugin;

class FilamentPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            // ...
            ->plugins([
                FilamentTranslatablePlugin::make()
                    ->supportedLocales([
                        'en' => 'English',
                        'nl' => 'Dutch',
                    ]),
            ]);
    }
}

Also you can specify localized tab labels PHP's locale_get_display_name() function to set the labels of the tabs to the display name of the active language.

use Go2Stone\FilamentTranslatable\Filament\Plugins\FilamentTranslatablePlugin;

class FilamentPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            // ...
            ->plugins([
                FilamentTranslatablePlugin::make()
                    ->supportedLocales([
                        'en' => 'English',
                        'nl' => 'Dutch',
                    ])
                    ->localeTabLabels(true),
            ]);
    }
}

By default, the package will use the app.locale if you don't specify the locales.

Combining with the official spatie-laravel-translatable-plugin?

This package is a replacement for the official on the create and edit pages only. If you are already using the official package, you will have to delete the use Translatable trait and the LocaleSwitcher header action from those pages:

-use Filament\Actions\LocaleSwitcher;
-use Filament\Resources\Pages\EditRecord\Concerns\Translatable;

class EditPage extends EditRecord
{
-    use Translatable;

    protected function getHeaderActions(): array
    {
        return [
-            LocaleSwitcher::make(),
            DeleteAction::make(),
        ];
    }
}

Usage

You can simply add ->translatable() to any field to make it translatable.

use Filament\Forms\Components\TextInput;

TextInput::make('name')
    ->label('Name')
    ->translatable(),

Disable translations dynamically

If you want to disable translations dynamically, you can set the first parameter of the ->translatable() function to true or false.

use Filament\Forms\Components\TextInput;

TextInput::make('name')
    ->label('Name')
    ->translatable(false),

Overwrite locales

If you want to overwrite the locales on a specific field you can set the locales through the second parameter of the ->translatable() function.

use Filament\Forms\Components\TextInput;

TextInput::make('name')
    ->label('Name')
    ->translatable(true, ['en' => 'English', 'nl' => 'Dutch', 'fr' => 'French']),

Locale specific validation rules

You can add locale specific validation rules with the third parameter of the ->translatable() method.

use Filament\Forms\Components\TextInput;

TextInput::make('name')
    ->label('Name')
    ->translatable(true, null, [
        'en' => ['required', 'string', 'max:255'],
        'nl' => ['nullable', 'string', 'max:255'],
    ]);

or

use Filament\Forms\Components\TextInput;

TextInput::make('name')
    ->label('Name')
    ->translatable(localeSpecificRules: [
        'en' => ['required', 'string', 'max:255'],
        'nl' => ['nullable', 'string', 'max:255'],
    ]);

Good to know

This package will substitute the original field with a Filament\Forms\Components\Tabs component. This component will render the original field for each locale.

All chained methods you add before calling ->translatable() will be applied to the original field. All chained methods you add after calling ->translatable() will be applied to the Filament\Forms\Components\Tabs component.

Laravel support

Laravel Version Package version
^11.0 ^1.0.2, ^2.0.0
^10.0 ^1.0.0, ^2.0.0

Changelog

Please see CHANGELOG for more information on what has changed recently.

Special Thanks to

License

MIT License (MIT). Read the License File for more information.

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-10