0

I am trying to set up localization on my website. However, when the user clicks on his preferred language, the default redirects function back() returns the default value of appLocale.

Request use to change language:

<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
    @if (app()->getLocale() !== 'en')
        <a href="/en" class="dropdown-item">
        <span>
            <img class="flag" src="{{asset('images/bg/gb.svg')}}" alt=""/> {{__('English')}}
        </span>
        </a>
    @endif
    @if (app()->getLocale() !== 'fr')
        <a href="/fr" class="dropdown-item">
        <span>
            <img class="flag" src="{{asset('images/bg/fr.svg')}}" alt=""/> {{__('Français')}}
         </span>
        </a>
    @endif
</div>

Route

Route::get('/{locale}', 'LangController@index');

LangController

class LangController extends Controller
{
    public function index($locale)
    {
        app()->setLocale($locale);

        return back()->permanent();
    }
}

When I check the value of the locale through dd(App::getLocale()) it is set to the new value but once the redirect function is executed, Its value moves back to the default local value from the config/app. Any idea on how to keep the change after redirecting.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
MNC Aubin
  • 11
  • 1
  • 3

0 Answers0