Answer the question
In order to leave comments, you need to log in
How to set up Locale Laravel correctly?
Middleware
class Localization
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Session::has('locale')){
App::setLocale(Session::get('locale'));
}
return $next($request);
}
}
<form action="{{route('language-switcher')}}" method="post" id="lang-switcher">
<div class="search-container">
<select name="change" id="font-options">
<option {{(App::isLocale('ru')) ? 'selected' : ''}} value="ru">RU</option>
<option {{(App::isLocale('en')) ? 'selected' : ''}} value="en">EN</option>
</select>
{{csrf_field()}}
<div class="search-arrow"></div>
</div>
</form>
// Controller
public function switcher(Request $request){
$locale = $request->change;
Session::put('locale', $locale);
return redirect()->back();
}
// Home
Route::get('/', '[email protected]')->name('index');
// About
Route::get('/about', '[email protected]')->name('about');
// Switcher lang
Route::post('/switcher-language/', '[email protected]')->name('language-switcher');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question