定制 filament/spatie-laravel-translatable-plugin 二次开发

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

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

filament/spatie-laravel-translatable-plugin

Composer 安装命令:

composer require filament/spatie-laravel-translatable-plugin

包简介

Filament support for `spatie/laravel-translatable`.

README 文档

README

Important

This plugin is now maintained by Lara Zeus: https://github.com/lara-zeus/translatable

Filament Spatie Translatable Plugin

Installation

Install the plugin with Composer:

composer require filament/spatie-laravel-translatable-plugin:"^3.2" -W

Adding the plugin to a panel

To add a plugin to a panel, you must include it in the configuration file using the plugin() method:

use Filament\SpatieLaravelTranslatablePlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->plugin(SpatieLaravelTranslatablePlugin::make());
}

Setting the default translatable locales

To set up the locales that can be used to translate content, you can pass an array of locales to the defaultLocales() plugin method:

use Filament\SpatieLaravelTranslatablePlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->plugin(
            SpatieLaravelTranslatablePlugin::make()
                ->defaultLocales(['en', 'es']),
        );
}

Preparing your model class

You need to make your model translatable. You can read how to do this in Spatie's documentation.

Preparing your resource class

You must apply the Filament\Resources\Concerns\Translatable trait to your resource class:

use Filament\Resources\Concerns\Translatable;
use Filament\Resources\Resource;

class BlogPostResource extends Resource
{
    use Translatable;
    
    // ...
}

Making resource pages translatable

After preparing your resource class, you must make each of your resource's pages translatable too. You can find your resource's pages in the Pages directory of each resource folder. To prepare a page, you must apply the corresponding Translatable trait to it, and install a LocaleSwitcher header action:

use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListBlogPosts extends ListRecords
{
    use ListRecords\Concerns\Translatable;
    
    protected function getHeaderActions(): array
    {
        return [
            Actions\LocaleSwitcher::make(),
            // ...
        ];
    }
    
    // ...
}
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateBlogPost extends CreateRecord
{
    use CreateRecord\Concerns\Translatable;
    
    protected function getHeaderActions(): array
    {
        return [
            Actions\LocaleSwitcher::make(),
            // ...
        ];
    }
    
    // ...
}
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditBlogPost extends EditRecord
{
    use EditRecord\Concerns\Translatable;
    
    protected function getHeaderActions(): array
    {
        return [
            Actions\LocaleSwitcher::make(),
            // ...
        ];
    }
    
    // ...
}

And if you have a ViewRecord page for your resource:

use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewBlogPost extends ViewRecord
{
    use ViewRecord\Concerns\Translatable;
    
    protected function getHeaderActions(): array
    {
        return [
            Actions\LocaleSwitcher::make(),
            // ...
        ];
    }
    
    // ...
}

If you're using a simple resource, you can make the ManageRecords page translatable instead:

use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageBlogPosts extends ListRecords
{
    use ManageRecords\Concerns\Translatable;
    
    protected function getHeaderActions(): array
    {
        return [
            Actions\LocaleSwitcher::make(),
            // ...
        ];
    }
    
    // ...
}

Setting the translatable locales for a particular resource

By default, the translatable locales can be set globally for all resources in the plugin configuration. Alternatively, you can customize the translatable locales for a particular resource by overriding the getTranslatableLocales() method in your resource class:

use Filament\Resources\Concerns\Translatable;
use Filament\Resources\Resource;

class BlogPostResource extends Resource
{
    use Translatable;
    
    // ...
    
    public static function getTranslatableLocales(): array
    {
        return ['en', 'fr'];
    }
}

Translating relation managers

First, you must apply the Filament\Resources\RelationManagers\Concerns\Translatable trait to the relation manager class:

use Filament\Resources\RelationManagers\Concerns\Translatable;
use Filament\Resources\RelationManagers\RelationManager;

class BlogPostsRelationManager extends RelationManager
{
    use Translatable;
    
    // ...
}

Now, you can add a new LocaleSwitcher action to the header of the relation manager's table():

use Filament\Tables;
use Filament\Tables\Table;

public function table(Table $table): Table
{
    return $table
        ->columns([
            // ...
        ])
        ->headerActions([
            // ...
            Tables\Actions\LocaleSwitcher::make(),
        ]);
}

Inheriting the relation manager's active locale from the resource page

If you wish to reactively inherit the locale of the Translatable resource page that the relation manager is being displayed on, you can override the $activeLocale property and add Livewire's Reactive attribute to it:

use Filament\Resources\RelationManagers\Concerns\Translatable;
use Filament\Resources\RelationManagers\RelationManager;
use Livewire\Attributes\Reactive;

class BlogPostsRelationManager extends RelationManager
{
    use Translatable;
    
    #[Reactive]
    public ?string $activeLocale = null;
    
    // ...
}

If you do this, you no longer need a LocaleSwitcher action in the table().

Setting the translatable locales for a particular relation manager

By default, the translatable locales can be set globally for all relation managers in the plugin configuration. Alternatively, you can customize the translatable locales for a particular relation manager by overriding the getTranslatableLocales() method in your relation manager class:

use Filament\Resources\RelationManagers\Concerns\Translatable;
use Filament\Resources\RelationManagers\RelationManager;

class BlogPostsRelationManager extends RelationManager
{
    use Translatable;
    
    // ...
    
    public function getTranslatableLocales(): array
    {
        return ['en', 'fr'];
    }
}

Publishing translations

If you wish to translate the package, you may publish the language files using:

php artisan vendor:publish --tag=filament-spatie-laravel-translatable-plugin-translations

filament/spatie-laravel-translatable-plugin 适用场景与选型建议

filament/spatie-laravel-translatable-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.43M 次下载、GitHub Stars 达 99, 最近一次更新时间为 2021 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.43M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 100
  • 点击次数: 29
  • 依赖项目数: 46
  • 推荐数: 0

GitHub 信息

  • Stars: 99
  • Watchers: 5
  • Forks: 48
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-01