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

ascanipek's avatar

Can not set localization with session

I am developing an application with multiple languages. I have no problem changing the language with an action taken from the frontend. The language files have been created and are working fine. However, I created urls in that language for content in different languages so that Google bots can scan the content, but although I set the language with the session, the page comes with the default language or the previously set language at the first opening, after refreshing it opens in the language of the url, but this does not solve my problem. I don't know if I'm missing a point about the Laravel lifecycle.

For English:

Route::get('/free-qr-code-generator', function () {
	session()->put('locale', 'en'); 
	session()->put('setted', 1); 
	return view('FrontEnd.QrGen')
});

For French:

Route::get('/generateur-de-code-qr-gratuit', function () {
	session()->put('locale', 'fr'); 
	session()->put('setted', 1); 
	return view('FrontEnd.QrGen'); 
});

For Deutsch:

Route::get('/kostenloser-qr-code-generator', function () {
	session()->put('locale', 'de'); 
	session()->put('setted', 1); 
	return view('FrontEnd.QrGen'); 
});

But it does not open with the set language at the first opening, but after the page is refreshed, it opens in the set language.

0 likes
5 replies
Snapey's avatar

Session is only stored at the end of the lifecycle, unless you use session()->save()

ascanipek's avatar

Thanks @snapey i have solved like this:

Route::get('/free-qr-code-generator', function () {
	App::setLocale('en'); 
    session()->put('locale', 'en'); 
    session()->put('setted', 1); 
    return view('FrontEnd.QrGen'); });

But this time localization middleware not working properly because App::setLocale('en'); forced frontend language change requests :(

My Language switcher middleware and rote are below:

public function handle($request, Closure $next)
    {
        if(session()->has('locale')){
            App::setLocale(session()->get('locale'));
        }
        
        return $next($request);
    }

   Route::get('/lang/{locale}', function ($locale) {
        session()->put('locale', $locale);
        session()->put('setted', 1);
        return redirect()->back();
    });
Snapey's avatar

did you try ?

Route::get('/free-qr-code-generator', function () {
	session()->put('locale', 'en'); 
	session()->put('setted', 1); 
    session()->save();
	return view('FrontEnd.QrGen')
});
ascanipek's avatar

Yes i try but first opening default language, opens in the setted language after refresh.

ascanipek's avatar

I just solved it like this

Route::get('/free-qr-code-generator', function () {
	if(!session()->has('static')){
		App::setLocale('en');
		session()->put('setted', 1); 
		session()->put('static', 1);
		return view('FrontEnd.QrGen');
	}
    else{
		App::setLocale(session()->get('locale')); 
		session()->put('setted', 1); 
		session()->put('static', 1); 
		return view('FrontEnd.QrGen');}
});

Route::get('/generateur-de-code-qr-gratuit', function () {
	if(!session()->has('static')){
		App::setLocale('fr');
		session()->put('setted', 1); 
		session()->put('static', 1);
		return view('FrontEnd.QrGen');
	}
    else{
		App::setLocale(session()->get('locale')); 
		session()->put('setted', 1); 
		session()->put('static', 1); 
		return view('FrontEnd.QrGen');}
});


Please or to participate in this conversation.