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

mstdmstd's avatar

How to implement localization in livewire SPA app?

In laravel 11 / livewire 3.5 app I want to localize 3 pages and I created my own middleware app/Http/Middleware/SetLocaleMiddleware.php:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Livewire\Attributes\On;
use Symfony\Component\HttpFoundation\Response;

/* Class set locale middleware by 'locale' session var */
class SetLocaleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        // I see these lines in log - so this middleware works ok
        \Log::info(varDump(session()->get('locale'), ' -1 OUT SetLocaleMiddleware session()->get(locale)::'));
        if (session()->has('locale')) {
            \Log::info(varDump(session()->get('locale'), ' -1 INSIDE SetLocaleMiddleware session()->get(locale)::'));
            App::setLocale(session()->get('locale'));
        }
        return $next($request);
    }
}

I added it to bootstrap/app.php :

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->appendToGroup('web', SetLocaleMiddleware::class);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

I switch locale in livewire component method as :

#[On('localeChanged')]
public function localeChanged(string $locale) {
    $this->locale = $locale;
    session()->put('locale', $this->locale);
    App::setLocale($this->locale);
    $this->dispatch('$refresh');
}

But locale is not changed.

Reading manual https://livewire.laravel.com/docs/upgrading#localization I modified routes/web.php:

Route::group(['prefix' => LaravelLocalization::setLocale()], function ()
{
    
    Route::get('/services/partnership-order/{locale?}', PartnershipOrderDialog::class)->name('services.partnership_order.index');
    Route::get('/services/pricing/{locale?}', Pricing::class)->name('services.pricing.index');
    Route::get('/{locale?}', QuizChooser::Class)->name('home');
    
});

But I did not catch what is class LaravelLocalization here ? I does not look like SetLocaleMiddleware (it has no setLocale method).

PS. Loooking at some localization examples I see that localizationis made with calling separate controller method and returning back. As I make SPA app I would prefer to implement localization without separate controller, but do not know can I do it ?

0 likes
1 reply
vincent15000's avatar
Level 63

When you change the locale in the livewire component, only the livewire component is refreshed and not the entire page.

So in this case, when you change the locale, it doesn't pass by the middleware.

Something else, this route doesn't to respect the common way to use internationalization : /services/partnership-order/{locale?}.

You should rather declare it like this : yourdomain.com/{locale}/services/partnership-order.

But I prefer not showing the locale in the route, I just store the locale in the session and I retrieve it from the session when I need it in the code.

1 like

Please or to participate in this conversation.