athulya's avatar

Laravel 8 Language switching inside a controller function.

I have an admin backend settings page, where I have added a dropdown field to select language. And I want to change the whole backend language according to the option selected on form submit.

Inside the submit function at the controller, I have done like this:

		$locale = $request['backend_language'];
        App::setlocale($locale);

This is working, but only changed the language of success message showing after successful submit. Every other part of the backend stands the same as before.

How can I modify this code to change full backend language?

0 likes
12 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You need to save it somewhere and then apply it in a service provider or middleware. You could store it in the users table to have it always be available. Or a session if users aren't signed in

athulya's avatar

@Sinnbeck If I create a middleware for this, can I call the middleware function, inside the controller function where I have done it now?

Sinnbeck's avatar

@athulya no you store the language in the controller. Either session or database. Then you redirect after setting it, to a page where it will then load the middleware

All your admin pages can use this middleware, but just skip setting the locale if the session is empty

athulya's avatar

@Sinnbeck my code Controller

		$switchLang = $this->DealerSetting->switchLang($request['backend_language']);

Service

		public function switchLang($lang)
		 { 
    			Session::put('applocale', $lang);
    			return Redirect::back();
		}

Middleware

public function handle($request, Closure $next)
{
    if (Session()->has('applocale')) {
        App::setLocale(Session()->get('applocale'));
    }
    else { // This is optional as Laravel will automatically set the fallback language if there is none specified
        App::setLocale(config('app.fallback_locale'));
    }
    return $next($request);
}

but no errors, not working. Did I miss anything?

Sinnbeck's avatar

@athulya Use dd to debug

dd(Session()->get('applocale'));
if (Session()->has('applocale')) {
        App::setLocale(Session()->get('applocale'));
    }
    else { // This is optional as Laravel will automatically set the fallback language if there is none specified
        App::setLocale(config('app.fallback_locale'));
    }
athulya's avatar

@Sinnbeck returns null, but it returns the value inside controller. Seems like middleware is not working.

athulya's avatar

@Sinnbeck It worked after I move the middleware after session in kernel.php

		\Illuminate\Session\Middleware\StartSession::class,
        \MuldenShop\Http\Middleware\Language::class,

Thank you so much Again!!

Tray2's avatar

@athulya now this is really shitty of you. You get a lot of help from @sinnbeck and when he has helped you Solve it you give the best answer to yourself instead of giving it to the reply that helped you solve it. Things like that doesn’t motivate ppl to help you.

athulya's avatar

@Tray2 I understood, But pls try to understand in my way. I am just in the learning stage. I am trying to find out solutions for my problems, So I have shared my doubts and I got help from Sinnbeck, But if I mark that as best answer, someone like me who is looking for examples to understand better would not get it. I posted my answer here, It may help others.

If this is wrong, I will correct.

Tray2's avatar

@athulya I understand your point of view and in some sense I agree with you. The "Best reply" isn't the best terminology perhaps it should have been "Most helpful" or something similar. I would have marked the post where @sinnbeck provided the link that gave you the help you needed to solve the issue.

My wording may have been a bit too harsh. What I meant was give credit where credit is due. Too many people on this forum doesn't give that and some don't even mark their threads as Solved.

We are all doing this for free in our spare time and the only thanks we get other than a thank you from some users and a few experience points for each Best answer.

1 like

Please or to participate in this conversation.