承接 tonsoo/filament-translatable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tonsoo/filament-translatable

最新稳定版本:0.1.0

Composer 安装命令:

composer require tonsoo/filament-translatable

包简介

Simple translatable models and Filament form helpers for Laravel.

README 文档

README

Model and Filament page traits for runtime translations.

Latest Version on Packagist Tests License

Locale discovery uses getTranslatableLocales() or the static $translatableLocales property when present, then app.available_locales (or app.locales), and finally the current app locale. Fallback translation uses app.fallback_locale when set.

Supported Versions

  • PHP: ^8.3
  • Laravel: ^13.0
  • Filament: ^5.0
  • Livewire: ^4.1

Installation

composer require tonsoo/filament-translatable

Model Trait

use Illuminate\Database\Eloquent\Model;
use Tonsoo\FilamentTranslatable\Concerns\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    protected array $translatable = ['title', 'content'];
}

Translatable attributes must be JSON columns.

Filament Page Trait

Use the page trait in your own create/edit pages:

use Filament\Resources\Resource;

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

    public static function getTranslatableLocales(): array // optional
    {
        return (array) config('app.available_locales', []);
    }
}

InteractsWithTranslatableResourceData reads translatable attributes from the model. Form inputs are scalar in the UI, and the trait converts translatable fields into locale maps for persistence.

Optional Runtime Locale Switcher

Add this resource trait to render a locale select beside the page heading:

use Tonsoo\FilamentTranslatable\Resources\Concerns\HasTranslatableLocaleSwitcher;

class ProductResource extends Resource
{
    use HasTranslatableLocaleSwitcher;

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

    public static function getTranslatableLocales(): array
    {
        return (array) config('app.available_locales', []);
    }

    protected static string $defaultTranslatableLocale = 'en'; // optional
    protected static array $translatableLocaleLabels = [ // optional
        'en' => 'English',
        'es' => 'Spanish',
    ];
}

You can also define the locale list with a static protected array $translatableLocales = [...] property instead of overriding getTranslatableLocales(). The locale labels array is optional; if you omit it, the switcher uses the locale code as the label. protected static string $defaultTranslatableLocale = 'en'; is optional and is used only when it matches one of the configured locales.

use App\Filament\Resources\ProductResource;
use Filament\Resources\Pages\CreateRecord;
use Filament\Resources\Pages\EditRecord;
use Tonsoo\FilamentTranslatable\Resources\Pages\Concerns\InteractsWithTranslatableResourceData;

class CreateProduct extends CreateRecord
{
    use InteractsWithTranslatableResourceData;

    protected static string $resource = ProductResource::class;
}

class EditProduct extends EditRecord
{
    use InteractsWithTranslatableResourceData;

    protected static string $resource = ProductResource::class;
}

This automatically wires:

  • mutateFormDataBeforeFill() on edit pages
  • mutateFormDataBeforeCreate() on create pages
  • mutateFormDataBeforeSave() on edit pages

Model API

$post->setTranslation('title', 'en', 'Hello');
$post->setTranslation('title', 'es', 'Hola');

$post->getTranslation('title', 'en');
$post->getTranslation('title', 'es'); // fallback uses app.fallback_locale
$post->getTranslations('title');

$post->title = 'Hello'; // current locale
$post->{'title.es'} = 'Hola'; // explicit locale

Locale-Aware Query Columns

When querying translatable attributes, the package maps plain translatable column names to the current locale JSON path.

app()->setLocale('pt_BR');

Post::query()->where('title', '=', 'Teste');
// same intent as querying: title->pt_BR = 'Teste'

You can also pass explicit locale dot notation:

Post::query()->where('title.pt_BR', '=', 'Teste');

Disable this behavior per query:

Post::query()
    ->withoutAutoTranslationsQuery()
    ->where('title->en', '=', 'Hello');

Re-enable later in the same chain:

Post::query()
    ->where('title', 'Teste')
    ->withoutAutoTranslationsQuery()
    ->where('title->en', 'Hello')
    ->withAutoTranslationsQuery()
    ->where('title', 'Teste');

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固