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

AjaxTest's avatar

Laravel Language Switcher

How can I change the language in laravel? I have wrote this code:

if ($request->lang === 'English') { config(['app.locale' => 'en']); } else { config(['app.locale' => 'ar']); }

in Controller But its not change the language

0 likes
8 replies
CorvS's avatar
CorvS
Best Answer
Level 27

@ajaxtest What exactly? Since I don't know what you are trying to achieve I am unable to help. If you set the locale as described above, the locale is changed for that request. So if you want the locale to be applied to all future requests (after using the language switch) you'd have to store the selected locale in the session and work with a middleware, which sets the locale to the value stored inside the session.

AjaxTest's avatar

I want to save the form data and check if the user had changed his language preference if yes, then change the language of all the pages.

CorvS's avatar

@ajaxtest Okay, then you probably want to store the locale inside the session when the user changed his preference and work with a middleware as described above or rather add a middleware that sets the locale to the user's preferred locale.

Please or to participate in this conversation.