Laralang

Laralang is the URL half of a multilingual Laravel app: one route definition becomes a real route per language, with a translated path where you want one, plus middleware to detect the locale. It does not touch the text on the page, which is a separate concern.
Installation
composer require edulazaro/laralang
A route per language
Instead of Route::get, use LocalizedRoute::get. The second argument is the list of locales, and any locale can carry its own translated path.
use EduLazaro\Laralang\LocalizedRoute;
LocalizedRoute::get('dashboard', [
'en',
'es' => 'panel',
], [DashboardController::class, 'index'])->name('dashboard');
That single definition registers /dashboard for English and /es/panel for Spanish.
- A locale listed as a bare value, like
'en', keeps the original path and only gets its prefix. - One written as
'es' => 'panel'gets a translated path.
The language prefix always goes at the front, even inside a Route::prefix('admin') group, so an admin route comes out as /admin/dashboard and /es/admin/panel.
Linking to them
Each localized route is named {locale}.{name}, so you can target a language explicitly.
route('en.dashboard'); // /dashboard
route('es.dashboard'); // /es/panel
route('dashboard') also works: it resolves to the current locale's version, so links follow whatever language the request is in without branching. Ziggy sees the routes too, so the same holds in JavaScript.
Detecting the locale
A localized URL is only useful if the app switches locale to match. Four middlewares ship with the package:
| Middleware | Source of the locale |
|---|---|
SetRouteLocale |
The URL prefix |
SetSessionLocale |
Whatever is stored in the session |
SetBrowserLocale |
Accept-Language on first visit, then remembered |
SetSmartLocale |
URL prefix first, then session or browser |
use EduLazaro\Laralang\Http\Middleware\SetSmartLocale;
Route::middleware(['web', SetSmartLocale::class])->group(function () {
// localized routes
});
They are separate on purpose: public routes and an admin panel can each detect the language the way that fits them.
Configuration
php artisan vendor:publish --tag="locales"
locales.php holds the supported languages and their URL prefixes. By default the first locale is the prefix-free default and every other locale prefixes with its own code, but any of them can be overridden, so Spanish could be /es while French is a bare path of your choosing.
Pairing with Laratext
Laralang localizes URLs; Laratext translates strings. The two halves stay cleanly separate and are useful independently.
built and maintained by Edu Lazaro · MIT license