1. Go to this page and download the library: Download laraveledge/laravel-locale library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bootstrap/app.php:
use Illuminate\Routing\Middleware\SubstituteBindings;
use Laraveledge\LaravelLocale\Middleware\SetLocale;
use Laraveledge\LaravelLocale\Middleware\EnsureIsLocale;
use Laraveledge\LaravelLocale\Middleware\SetDefaultLocaleForUrls;
->withMiddleware(function (Middleware $middleware): void {
$middleware->prependToPriorityList(
before: SubstituteBindings::class,
prepend: SetDefaultLocaleForUrls::class,
);
$middleware->alias([
'ensureIsLocale' => EnsureIsLocale::class,
'setDefaultUrls' => SetDefaultLocaleForUrls::class,
'setLocale' => SetLocale::class,
]);
})
web.php:
Route::group([
'prefix' => '{locale}',
'middleware' => ['web', 'ensureIsLocale', 'setLocale', 'setDefaultUrls'] // ensure this middleware order
], function () {
Route::get('/', function () {
return 'home';
});
Route::get('/about', function () {
return 'products';
});
Route::get('/test', function(){
return 'test';
})->name('test'); //ensure a route named test is present in your routes so you can check/test the Locale::debug() mnethod
Route::fallback(function () {
abort(404, 'Hm, why did you land here somehow?');
}); //Neccessary , why ? see Edge Cases below
});
Route::fallback(function () {
abort(404, 'Hm, why did you land here somehow?');
});
---