unusualdope/filament-model-translatable

1.0.0 2024-03-27 04:45 UTC

This package is auto-updated.

Last update: 2024-09-12 04:56:47 UTC


README

描述

添加了使用语言表插入翻译的功能。例如,您可以创建一个主模型,存储对象ID和不可翻译的数据,在lang模型中,您必须定义主对象的键(在这种情况下为post_id)以及始终为"language_id"的语言键。以下是在蓝图包中定义模型示例的YAML示例

  Post:
    user_id: string
    postStatus_id: string nullable
    relationships:
      belongsTo: PostStatus, \App\Models\User
      hasMany: PostLanguage

  PostLanguage:
    title: string:160
    content: string nullable
    post_id: unsignedInteger
    language_id: unsignedInteger
    relationships:
      belongsTo: Language

安装

简单地使用composer安装

composer require unusualdope/filament-model-translatable

然后运行

php artisan fmt:install

并按照提示发布和运行迁移以及创建语言。

别忘了在您的面板中注册插件

use Unusualdope\FilamentModelTranslatable\FmtPlugin;
use Filament\Panel;

class AdminPanelProvider extends PanelProvider
{
   public function panel(Panel $panel): Panel
   {
       return $panel
           // ...
           ->plugin(
               FmtPlugin::make()
           );
   }
}

主要模型

在主模型中扩展FmtModel

use Unusualdope\FilamentModelTranslatable\Models\FmtModel;

class Post extends FmtModel
{
    //...

定义一些属性以使插件工作,请参阅带有注释的示例

    /**
     * Translatable props needed
     */

    protected $lang_model = 'App\Models\PostLanguage'; //fqn of the translatable model 
    protected $lang_foreign_key = 'post_id'; //foreign key

    protected $is_translatable = true;
    protected $translatable = [
        'title' => [ //field name that will match with the LangModel db field and property
            'formType' => 'TextInput', //The type of form input field as in Filament
            'name' => 'Title', //Field Label
            'methods' => [ //The methods you want to call from filament on your field to define it
                'required' => '1',
                'prefix' => 'title',
                ...
                'anotherMethod' => [
                    'param1' => '1',
                    'param2' => 'test'
                    ...
                    'paramN' => 'xxx'
                ]
            ],
        ],
        'content' => [
            'formType' => 'RichEditor',
            'name' => 'Content',
            'methods' => [
                'columnSpanFull' => '',
            ],
        ],
    ];

资源

在RESOURCE中,您必须使用Trait fmtTrait并使用以下方式检索可翻译字段

self::addTranslatableFieldsToSchema(array $schema, Form $form, Bool false);

1 - 作为第一个参数,您可以传递当前架构,它将返回带有附加可翻译字段的完整架构。

2 - 表单对象

3 - 如果您只想返回包含可翻译字段架构的数组(以便您将其放置在架构的中间),则将其设置为false(默认为true)。以下只是一个示例

class PostResource extends Resource
{
    use FmtModelTrait;

    protected static ?string $model = Post::class;

    protected static ?string $navigationIcon = 'heroicon-o-document-duplicate';

    public static function form(Form $form): Form
    {

        $schema = [
            Forms\Components\Select::make('user_id')
                ->relationship('user', 'name')
                ->searchable()
                ->required(),
            Forms\Components\Select::make('post_status_id')
                ->relationship('postStatus', 'name')
                ->required(),
        ];

        $schema = self::addTranslatableFieldsToSchema($schema, $form);

        return $form
            ->schema($schema);
    }

创建和编辑资源页面

在创建页面上扩展

Unusualdope\FilamentModelTranslatable\Filament\Resources\Pages\FmtCreateRecord

而不是标准的 Filament\Resources\Pages\CreateRecord

在编辑页面上扩展

Unusualdope\FilamentModelTranslatable\Filament\Resources\Pages\FmtEditRecord

而不是标准的 Filament\Resources\Pages\EditRecord

例如。

use Unusualdope\FilamentModelTranslatable\Filament\Resources\Pages\FmtCreateRecord;

class CreatePlan extends FmtCreateRecord
{
    protected static string $resource = PlanResource::class;
}

结果

您将获得一个标签页,允许您更改语言并为每种语言填写内容

Fmt Preview Image

问题或建议

请随时提出改进建议或直接在github 插件仓库 报告任何问题