- Laravel 7.0 or higher
- Composer is required for installation
Install the library using Composer:
$ composer require richarddobron/laravel-localized-routes$ php artisan vendor:publish --provider="richarddobron\LocalizedRoutes\LocalizedRouteServiceProvider" --tag="config"This will create config/localized-routes.php.
<?php
return [
'prefix' => true,
'locales' => [
'de',
'it',
// ...
],
'route_key' => 'locale',
'route_locale' => 'en',
'redirect_code' => 302,
];Create a translation file like routes/lang/de/routes.php:
<?php
return [
'example' => 'beispiel',
];Then in routes/web.php:
Route::localeGroup([], function () {
Route::get('/example', function () {
return 'Hello, World!';
});
});This makes these URLs:
/example(default)/de/beispiel/it/example
You can give different paths per language:
Route::get('/book/{sku}', function (string $sku) {
return 'Book details ' . $sku;
})->name('book')->locale([
'de' => 'buch/{sku}',
'it',
]);This makes these URLs:
/book/{sku}(default)/de/buch/{sku}/it/book/{sku}
You can customize the library with the following configuration options.
| Option | Description | Default |
|---|---|---|
prefix |
Automatically prepends locale codes (/de, /it, etc.) to routes. |
true |
locales |
List of supported locales. | [] |
route_key |
The route parameter key used for locale identification. | locale |
route_locale |
Locale used for default routes. | en |
redirect_code |
HTTP status code used for redirecting to the correct localized route. | 302 |
$ composer testsPlease see CONTRIBUTING for details.
This project is licensed under the MIT License. See the LICENSE file for details.