Search code examples
phplaravelmultilingual

LARAVEL Redirect user to same page they are viewing when language change link is clicked in the web page


I am new to Laravel and the scenario is like this: I have a bilingual site built using laravel and my problem lies in let's say, for example, the user is viewing about us page and he chose to change language of the about us page to french. Now what i want to do is redirect the user to the same page url (about us) page even after language change link is clicked. And my language changing logic is based on setting session variable i.e. if sesssion[$lang]= 1 fetch english content and if 2 fetch french content. My daunting problem lies in capturing the currently active page URL and redirect the user to the same URL after changing the language to french !!!!! And i don't even know is my change language logic standard one or not


Solution

  • An easy way is to use the redirect()->back() method to go back where you were , but i believe a better practice is to define language also through routes example

    Route::get('about_us/{lang}','YourController@method_name');
    

    and grouping them by middleware to switch between languages and redirecting to about_us/ar , about_us/en or whatever but you have mentioned that you are using sessions so just

    redirect()->back()

    after changing language .