Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Filament Translate Field Laravel Package

solution-forest/filament-translate-field

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require solution-forest/filament-translate-field
    php artisan filament:assets
    php artisan optimize
    
  2. Basic Usage: Add the TranslateField to a Filament form/resource:

    use SolutionForest\FilamentTranslateField\Fields\TranslateField;
    
    TranslateField::make('title')
        ->languages(['en', 'fr', 'de'])
        ->required(),
    
  3. First Use Case:

    • Create a translatable model (e.g., Post with title and content fields).
    • Register a Filament resource for the model and replace standard text fields with TranslateField.

Implementation Patterns

Core Workflows

  1. Multi-Language Forms:

    TranslateField::make('description')
        ->languages(config('app.locales'))
        ->columnSpanFull(),
    
  2. Conditional Translations:

    TranslateField::make('name')
        ->languages(fn ($record) => $record->available_locales)
        ->defaultLanguage('en'),
    
  3. Integration with Existing Models:

    • Use with models already using spatie/laravel-translation-loader or spatie/laravel-translatable.
    • Ensure no conflict by avoiding Translatable trait on Filament EditPage (per README warning).
  4. Dynamic Language Selection:

    TranslateField::make('bio')
        ->languages(fn () => Language::query()->pluck('code')->toArray()),
    

Advanced Patterns

  • Nested Translations:
    TranslateField::make('metadata.key')
        ->languages(['en', 'es'])
        ->json(),
    
  • Validation:
    TranslateField::make('slug')
        ->languages(['en', 'fr'])
        ->rules([
            'en' => 'required|unique:posts,slug',
            'fr' => 'required|unique:posts,slug_fr',
        ]),
    
  • Livewire Integration:
    TranslateField::make('live_content')
        ->languages(['en', 'ja'])
        ->livewire(),
    

Gotchas and Tips

Pitfalls

  1. Trait Conflict:

    • Error: Translatable trait conflicts with filament/spatie-laravel-translatable-plugin on EditPage.
    • Fix: Remove use Spatie\Translatable\HasTranslations; from the model or use CreatePage/UpdatePage separately.
  2. Database Schema:

    • Issue: Missing locale column in pivot tables for many-to-many relationships.
    • Fix: Add locale column and update migrations:
      Schema::table('post_tag', function (Blueprint $table) {
          $table->string('locale')->nullable();
      });
      
  3. Asset Loading:

    • Problem: Translations not saving after filament:assets not run.
    • Fix: Always run php artisan filament:assets after installation.

Debugging Tips

  • Check Logs:
    tail -f storage/logs/laravel.log | grep "TranslateField"
    
  • Validate JSON: Ensure json() fields are valid JSON arrays/objects per language.

Extension Points

  1. Custom Rendering: Override the view:

    TranslateField::make('custom_field')
        ->view('filament-translate-field::custom-field'),
    
  2. Language Switcher: Extend the language selector:

    TranslateField::make('extended_title')
        ->languages(['en', 'zh'])
        ->languageSelectorView('filament-translate-field::custom-selector'),
    
  3. Middleware: Restrict translations to authenticated users:

    TranslateField::make('admin_only')
        ->middleware([Authorize::class]),
    

Performance

  • Batch Updates: Use updateOrCreate for bulk translations:
    $post->translations()->updateOrCreate(
        ['locale' => 'fr'],
        ['title' => 'Nouveau titre']
    );
    
  • Caching: Cache language lists if dynamic:
    Cache::remember('app.locales', now()->addHours(1), fn () => config('app.locales'));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle