Skip to main content
Back to packages
11 GitHub stars

niels-numbers/laravel-localizer

View on GitHub

Description

Locale-aware routing for Laravel: auto-detect, auto-redirect, and resolve route() per language.

1. Set your supported locales

Make sure your default is set in config/app.php:

'fallback_locale' => 'en',

Publish the package config:

php artisan vendor:publish --provider="NielsNumbers\\LaravelLocalizer\\ServiceProvider" --tag=config
// config/localizer.php
return [
'supported_locales' => ['en', 'de', 'fr'],
// ...
];

2. Register the middleware

Laravel 11+ (bootstrap/app.php):

->withMiddleware(function (Middleware $middleware) {
$middleware->web(remove: [
\Illuminate\Routing\Middleware\SubstituteBindings::class,
]);
$middleware->web(append: [
\NielsNumbers\LaravelLocalizer\Middleware\SetLocale::class,
\NielsNumbers\LaravelLocalizer\Middleware\RedirectLocale::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
]);
})

SetLocale has to run after StartSession (so session-based locale detection works) and before SubstituteBindings (so translated route bindings like {post:slug} resolve against the correct locale).

3. Wrap your routes

Route::localize(function () {
Route::get('/about', AboutController::class)->name('about');
});

Produces:

  • /about (default locale, prefix hidden)
  • /de/about, /fr/about, ...

php artisan route:list shows both as static routes:

GET|HEAD about ............... without_locale.about AboutController
GET|HEAD {locale}/about .......... with_locale.about › AboutController

Related Content on Laravel Daily

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.