0
votes

I made a Laravel application with the language switch. that after a language change the app redirect you back after the language change on the site. the code is as follow:

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

But i got the problem if someone send the link on facebook or other social media as example www.example.com/lang/en than the application redirect me back to the facebook but not the main application.

I was searching around how to do this better but always i saw hard coded things like extra URL as example www.example.com/lang/change/en with another function.

But i think tehere should be a way to do this better with an if checking the request but actually i can check the request url but i get always the app url back and nothing more.

So my question is how can i made so that if someone enter te url from other site than the application is will redirect me to the www.example.com/ (With the langage from the sended request like www.example.com/lang/en and not back to the facebook or like that) and when it happens on the application site will return me back to the previuse site where i was like www.example.com/post/3

I'm a starter after the school so do not be mad that i do not know everthing If someone can help me i would be thankfull!

1

1 Answers

0
votes

If it's a possibility, do that with javascript or vue, and then just reload the window location.

HTML:

<a href="javascript:void(0);" data-lang="es" class="lang">EspaƱol</a>
<a href="javascript:void(0);" data-lang="en" class="lang">English</a>
<a href="javascript:void(0);" data-lang="it" class="lang">Italiano</a>

JQuery:

$(document).on("click",".lang", function () {
    $.get('/lang/' + $(this).data('lang')).then((res) => {
        location.reload();
    });
});

EDIT: For extra clarity, this is your lang function, you don't need to return a redirect.

public function lang($locale)
{
    App::setLocale($locale);
    session()->put('locale', $locale);
    return response()->json('ok');
}