Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sinnbeck's avatar
Level 102

Livewire validation localization

I have a fullpage livewire component where a guest user can download a file. The link to the page contains the locale the page should use, and I use this to set the locale in the mount() method. This works for all content on the page.

Now the problem is that the validation errors does not get translated. It just shows in english.

have tried setting the locale in boot(), rules(), middleware, $this->withValidator(), just before calling $this->validate() but none of them work I'm curious how I can hook into it correctly

0 likes
3 replies
Tray2's avatar

From the docs.

#[Validate('required', message: 'Please provide a post title', translate: false)]
public string $title;

I think changing it to true should work.

#[Validate('required', message: 'Please provide a post title', translate: true)]
public string $title;
Sinnbeck's avatar
Level 102

@Tray2 Yeah that what I am doing but when I dd() inside the BaseValidate class (the boot() method), it gives me the wrong locale :)

dd(App::getLocale()); //returns en

This is my properyy

#[Validate('accepted', message: 'access.validation_failed', translate: true)]
public bool $accept = false;
Sinnbeck's avatar
Sinnbeck
OP
Best Answer
Level 102

I seem to have figured out a way of doing it using middleware.

I added this to my AppServiceprovider to tell livewire to persist the middleware

Livewire::addPersistentMiddleware([
      \App\Http\Middleware\SetLocale::class,
]);

and this to the route

Route::get('/access/{access:ulid}',
    Access::class,
)
    ->name('access')
    ->middleware(\App\Http\Middleware\SetLocale::class); //this
1 like

Please or to participate in this conversation.