Norbertho
1 month ago
253
5
Laravel

Store users selected language option

Posted 1 month ago by Norbertho

Hi, I am creating an app.and currently I store the user selected language option in session and it works perfectly for example if user selects English language then I store the 'en' in session with the following controller:

    public function index($locale){
        App::setlocale($locale);
        session()->put('locale', $locale);
        return redirect()->back();
    }

and I do the following in a middleware:

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

It works fine user can navigate trough the app back and forward and the selected language is stay as users selected. I can log in into app and the selected language option is stay as selected and stored in session. However when the user logout from the app and redirected to the welcome page then the session is reset and the selected language cleared and the default language is set. Is there any way to prevent the logout function to clear the selected language optino?

Please sign in or create an account to participate in this conversation.