D
D
dfgdfgdfgdfgfg2019-12-10 19:15:35
Laravel
dfgdfgdfgdfgfg, 2019-12-10 19:15:35

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);
    }
}

Select
<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();
    }

route
// 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');

Question:
At the moment, the location is determined through the session and loading the desired content, how to make the main language (ru) have a link / about and in English. (en) /en/about

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question