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

Alfonso Rodriguez's avatar

How update localization

Hello everyone! Does anyone know how to correctly execute localization in Laravel 9 + Vue 3+ Inertia? Currently I have a select where I make a post to a function to update the localization in this way, but I have to refresh the entire site to see the change.

LocaleController

public function store(StoreLocalRequest $storeLocalRequest, string $locale)
    {
        try {
            app()->setLocale($locale);
            session()->put('locale', $locale);
            Inertia::share('locale', $locale);
        } catch (Exception $e) {
            Log::error($e);
            dd($e);
            return redirect()->back()->with('alert', [
                'type' => 'error',
                'code' => trans('app.admin.locale.error-update')
            ]);
        }

        return redirect()
            ->back()
            ->with('alert', [
                'type' => 'success',
                'code' => trans('app.admin.locale.success-update')
            ]);
    } 

HandleInertiaRequests

 public function share(Request $request)
    {
        $params = [];
        $params['unreadMessages'] = auth() ? SaleContact::where('is_read', false)->count() : 0;

        if ($request->session()->has('alert')) {
            $params['alertMessage'] = $request->session()->get('alert');
        }

        $params['ziggy'] = new Ziggy();
        $params['currentRoute'] = $request->route()->getName();
        $params['currentYear'] = Carbon::now()->year;
        $params['currentLanguage'] = session()->has('locale') ? session()->get('locale') : config('app.locale');
        $params['supportLanguages'] = config('locales');
        return array_merge(parent::share($request), $params);

Locale (middleware)

public function handle(Request $request, Closure $next)
    {
        app()->setLocale(config('app.locale'));
        if (session()->has('locale')) {
            app()->setLocale(session()->get('locale'));
        }

        return $next($request);
    }

Submit from component vue

changeLanguage() {
            this.$inertia.post(
                this.route("public.locale.store", {
                    locale: this.selected.value,
                }),
                {
                    preserveState: true,
                }
            );
        },

Note that when making the post, HanlderInertia is not updating with the session variable. I'm not really sure where else to look if I have something wrong.

0 likes
1 reply
rodrigo.pedra's avatar

As your localization is done server-side, you will need a full reload.

But, you can tell inertia to do a full reload for you, instead of asking the users to perform a manual reload.

Just return an Inertia's external response, to the previous page, and inertia will make a full reload for you:

public function store(StoreLocalRequest $storeLocalRequest, string $locale)
{
    try {
        app()->setLocale($locale);
        session()->put('locale', $locale);
        Inertia::share('locale', $locale);
    } catch (Exception $e) {
        Log::error($e);
        dd($e);
        return redirect()->back()->with('alert', [
            'type' => 'error',
            'code' => trans('app.admin.locale.error-update')
        ]);
    }

    $response = redirect()->back()->with('alert', [
        'type' => 'error',
        'code' => trans('app.admin.locale.error-update')
    ]);

    // Inertia is smart enough to identify
    // the redirect URL from the response object
    return Inertia::location($response);
}

https://inertiajs.com/redirects#external-redirects

1 like

Please or to participate in this conversation.