Skip to main content

Localized routes

laravel-lang/routes selects the locale at request time and generates URLs for localized route groups. The package is included in Common.

composer require laravel-lang/routes

Make sure the locales used in your URLs are installed. Configure parameter names, middleware, and default-locale behavior in config/localization_routes.php.

Register a localized route group​

In routes/web.php, use the Route::localizedGroup() macro. The LaravelLang\Routes\Facades\LocalizationRoute::group() facade provides the same grouping method.

use Illuminate\Support\Facades\Route;

Route::localizedGroup(function (): void {
Route::get('/products/{product}', fn (string $product) => view('products.show', [
'product' => $product,
]))->name('products.show');
});

The callback registers both URL patterns under the default configuration.

PatternRoute name
/products/{product}products.show
/{locale}/products/{product}localized.products.show

The locale middleware consumes the locale parameter before invoking the route handler. A request for an unknown or uninstalled URL locale redirects to the unprefixed route when the route is named.

Generate a localized URL​

use function LaravelLang\Routes\localizedRoute;

$url = localizedRoute('products.show', [
'product' => 42,
'locale' => 'fr',
], absolute: false);
// /fr/products/42

The helper uses the configured locale parameter name and prefers the prefixed route if one exists. It passes a supplied locale string through to URL generation. To generate the URL with an alias, pass that alias or resolve it first with Locales::toAlias(). If you omit the locale, the helper uses Locales::fallback(). To use the current locale, pass Locales::current() explicitly.

localization_routes.hide omits the fallback locale from URLs generated by the helper. localization_routes.redirect redirects requests for the current locale from the prefixed group to the unprefixed route. Both settings default to false.

Select a locale with middleware​

Each middleware can be applied directly by its class from LaravelLang\Routes\Middlewares.

MiddlewareSource
LocalizationByParameterMiddlewareA route parameter, locale by default
LocalizationByParameterWithRedirectMiddlewareA route parameter. With a named route and a declared parameter, it substitutes the current locale when the requested one is missing or uninstalled
LocalizationByCookieMiddlewareAn Accept-Language cookie by default
LocalizationByHeaderMiddlewareThe first installed locale in Accept-Language preference order. It tries the full tag before its base language and skips wildcards and entries with quality zero
LocalizationBySessionMiddlewareAn Accept-Language session key by default
LocalizationByModelMiddlewareThe authenticated Eloquent user's locale attribute by default. Append :guard_name to the middleware class to select a guard

For the prefixed group, the default order is URL parameter, cookie, header, session, then model. The unprefixed group uses the same order without the URL step. A later valid source can override an earlier one. A locale change updates Laravel and persists the canonical code in the configured session key. Use the web middleware group when you need sessions and cookies. The header middleware requires PHP Intl.

When the middleware changes the locale, it dispatches LaravelLang\Routes\Events\LocaleHasBeenSetEvent. Its locale property is a LaravelLang\LocaleNames\LocaleData object. Use $event->locale->code for the canonical code.