Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

The language switches for one page in Laravel, then falls back to the default

Your error

App::setLocale('fr') works on the switch route, but the next page is back in the default language2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The locale is set for one request and never stored

Save the choice in the session in the switch route, and set it again on every request in a small middleware.

Fix
// routes/web.php
Route::get('/lang/{locale}', function (string $locale) {
    if (! in_array($locale, ['en', 'es', 'fr'])) {
        abort(400);
    }
    session(['locale' => $locale]);
    return back();
});

// app/Http/Middleware/SetLocale.php
public function handle(Request $request, Closure $next): Response
{
    if ($locale = $request->session()->get('locale')) {
        App::setLocale($locale);
    }
    return $next($request);
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

App::setLocale() changes the language for the current HTTP request only, so a switch route that sets it and redirects shows the default language again on the next page. Store the chosen locale in the session and apply it in a middleware appended to the web group, after StartSession. Changing config('app.locale') at runtime is not a substitute. Applies to Laravel 12 and 13.

  • Applies to Laravel 13.43 (PHP 8.3 to 8.5), Laravel 12 (PHP 8.2 to 8.5)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Add a throwaway route that prints the active locale and the stored choice, switch the language, then open it. en with fr stored means the choice is saved but not applied; en with null means it is never stored or the middleware cannot see the session.

Run this first
// routes/web.php (remove after debugging)
Route::get('/locale-debug', fn () => [App::currentLocale(), session('locale')]);

Then find the line you see in this list and open its fix:

  1. 1The locale is set for one request and never storedThe switch route calls App::setLocale() and redirects, and no session key holds the choice
  2. 2The locale middleware runs before the session is startedThe session holds the locale, but inside the middleware session('locale') is null; the middleware was added with $middleware->append() in bootstrap/app.php
  3. 3The code changes config('app.locale') instead of calling setLocale()config('app.locale') returns the new value, but __() still translates into the old language

What does the error look like?

  • App::setLocale('fr') works on the switch route, but the next page is back in the default language
  • session('locale') is null inside the middleware that should apply the locale
  • App::currentLocale() still returns en after config(['app.locale' => 'fr'])

Cause 1 of 3

The locale is set for one request and never stored

You seeThe switch route calls App::setLocale() and redirects, and no session key holds the choice

Check

When setLocale works once and the language resets on the next page, the change was never persisted. The localization docs describe App::setLocale() as changing the default language for a single HTTP request; the redirect after the switch is a new request, which starts again from APP_LOCALE.

Check
grep -rn "setLocale" routes/ app/

Fix

Save the choice in the session in the switch route, and set it again on every request in a small middleware.

Fix
// routes/web.php
Route::get('/lang/{locale}', function (string $locale) {
    if (! in_array($locale, ['en', 'es', 'fr'])) {
        abort(400);
    }
    session(['locale' => $locale]);
    return back();
});

// app/Http/Middleware/SetLocale.php
public function handle(Request $request, Closure $next): Response
{
    if ($locale = $request->session()->get('locale')) {
        App::setLocale($locale);
    }
    return $next($request);
}

Verify

After switching, the debug route shows the new locale on every following page load.

Verify
curl -s -b cookies.txt -c cookies.txt https://example.com/locale-debug

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

The locale middleware runs before the session is started

You seeThe session holds the locale, but inside the middleware session('locale') is null; the middleware was added with $middleware->append() in bootstrap/app.php

Check

A locale middleware registered as global middleware cannot read the session yet. Global middleware wraps the whole request before routing, while StartSession belongs to the web middleware group that runs for the matched route, so at that point the stored choice is not loaded.

Check
grep -n "SetLocale" bootstrap/app.php

Fix

Register the middleware in the web group, which places it after StartSession, or insert it right after StartSession explicitly.

Fix
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
    $middleware->web(append: [
        \App\Http\Middleware\SetLocale::class,
    ]);
})

Verify

The debug route now reports the stored locale as the active one.

Verify
curl -s -b cookies.txt https://example.com/locale-debug   # ["fr","fr"]

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

The code changes config('app.locale') instead of calling setLocale()

You seeconfig('app.locale') returns the new value, but __() still translates into the old language

Check

Writing the config value at runtime does not switch the translator that is already in use. App::setLocale() updates app.locale, tells the translator the new locale and fires the LocaleUpdated event; config(['app.locale' => ...]) only does the first of those three.

Check
grep -rn "app.locale' =>" app/ routes/

Fix

Replace the config write with App::setLocale(), and read the active value with App::currentLocale().

Fix
use Illuminate\Support\Facades\App;

App::setLocale($locale);
$active = App::currentLocale();

Verify

A translated string comes back in the selected language on the same request.

Verify
dd(App::currentLocale(), __('messages.welcome'));

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Keep one SetLocale middleware in the web group as the only place that applies the locale on each request.
  • Validate the requested locale against an allow-list before storing it, as the localization docs do, so a crafted URL cannot set an unsupported language.
  • Cover the switch with a feature test: call /lang/fr, then a second route, and assert App::currentLocale() is fr.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.