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 showFix it fast
Most common cause, 1 of 3The 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.
// 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);
}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.
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.
// 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:
- 1The locale is set for one request and never stored
The switch route calls App::setLocale() and redirects, and no session key holds the choice - 2The locale middleware runs before the session is started
The session holds the locale, but inside the middleware session('locale') is null; the middleware was added with $middleware->append() in bootstrap/app.php - 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 languagesession('locale') is null inside the middleware that should apply the localeApp::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.
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.
// 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.
curl -s -b cookies.txt -c cookies.txt https://example.com/locale-debugConfirmed by laravel.com
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.
grep -n "SetLocale" bootstrap/app.phpFix
Register the middleware in the web group, which places it after StartSession, or insert it right after StartSession explicitly.
// 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.
curl -s -b cookies.txt https://example.com/locale-debug # ["fr","fr"]Confirmed by laravel.com
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.
grep -rn "app.locale' =>" app/ routes/Fix
Replace the config write with App::setLocale(), and read the active value with App::currentLocale().
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.
dd(App::currentLocale(), __('messages.welcome'));Confirmed by raw.githubusercontent.com
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.