Answer the question
In order to leave comments, you need to log in
How to make language selection in Laravel?
I am making one site and I need to make it multilingual, I found this code in the official Laravel 8 documentation:
use Illuminate\Support\Facades\App;
Route::get('/greeting/{locale}', function ($locale) {
if (! in_array($locale, ['en', 'es', 'fr'])) {
abort(400);
}
App::setLocale($locale);
});
Route::get('/', function () {
return view('welcome');
});
Route::get('/{locale}', function ($locale) {
if (! in_array($locale, ['en', 'ru'])) {
abort(400);
}
App::setLocale($locale);
});
Answer the question
In order to leave comments, you need to log in
I wouldn't try to support paths with and without language.
I think it's enough to / redirect to / ru, as habr.com does, for example, and all other links should always be done with the language.
Those. /en/posts/1 is fine, but /posts/1 is already an error.
Further, I would wrap everything, in general, all the routes in a group
Route::prefix('{lang}')->group(function () {
//...
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question