filament: Repeater does not work with translations

Package

filament/spatie-laravel-translatable-plugin

Package Version

v3.0.40

Laravel Version

v10.22.0

Livewire Version

v3.0.2

PHP Version

PHP 8.2.9

Problem description

I have a form for editing a model with a Repeater for a hasMany relation. With filamentphp/spatie-laravel-translatable-plugin the translation of all translatable fields of the main model works like a charm, but the translation of the translatable fields from the related models does not work. When I switch the language with the language switcher, the fields of the main model gets updated accordingly, but not inside of the repeater.

image

Also after changing a translatable text from one of the related model in the Repeater, all languages gets overwritten. Eg editing the german translation results from {"en":"Text EN","de":"Text DE"} to {"en":"New Text DE","de":"New Text DE"}.

Expected behavior

Expected behavior would be that it is possible to translate the translatable fields inside of a Repeater form element and that it behaves like the fields of the main model itself.

Steps to reproduce

To reproduce this issue you can use the linked minimal demo project which provides a Post model having many Comments. The PostResource ist setup in a way that the Comments of a Posts are in a Repeater form field. Translating the Post itself works, but translating the comments does not.

Reproduction repository

https://github.com/pdaether/filament-translation

Relevant log output

No response

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Reactions: 11
  • Comments: 22 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I would love the fix for this, I’ve tried all of the suggestions above, but none of them worked for me. Some even yielded weird results, like for example the suggestion from @chosten was saving repeater items ON LOCALE CHANGE, and with one same value for all locales.

I’ve discovered a way to resolve this issue. You just need to add the names of the relations to the $translatable property in the Post Model like this:

public $translatable = ['title', 'comments'];

This adjustment should solve the problem smoothly.

Is there any fix except using inline translation labels.

@tahacankurt Hi! As a quick fix you can override updatedActiveLocale method of the EditRecord\Concerns\Translatable trait. For example, for this demo repository, EditPost.php should look like this:


<?php

namespace App\Filament\Resources\PostResource\Pages;

use App\Filament\Resources\PostResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Arr;

class EditPost extends EditRecord
{
    use EditRecord\Concerns\Translatable;

    protected static string $resource = PostResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\DeleteAction::make(),
            Actions\LocaleSwitcher::make(),
        ];
    }

    public function updatedActiveLocale(): void
    {
        if (blank($this->oldActiveLocale)) {
            return;
        }

        $this->resetValidation();

        $translatableAttributes = static::getResource()::getTranslatableAttributes();

        $this->otherLocaleData[$this->oldActiveLocale] = Arr::only($this->data, $translatableAttributes);

        // Fix part - Start
        $this->form->fill([
            ...Arr::except($this->data, $translatableAttributes),
            ...$this->otherLocaleData[$this->activeLocale] ?? [],
        ]);
        // Fix part - End

        unset($this->otherLocaleData[$this->activeLocale]);
    }
}`

@corept I am investigating a temporary solution with storing the activeLocale in session and forcing a reload of the page, but it is messy.

@MahdiJalilvandir 👍 Perfect, I’ve tested this and it solved the problem for me as well.

Hi @pdaether, Did you ever figure this one out?