承接 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.

统计信息

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

GitHub 信息

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

其他信息

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