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:
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?
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 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
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);
}
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 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.
@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.
@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.