chinleung/laravel-locales
Add multi-locale support to Laravel with simple config and helper functions. Define supported locales via app.locales or a published config, and use locale() to get/set the current locale and locales() to get/set supported locales.
composer require chinleung/laravel-localesphp artisan vendor:publish --provider="ChinLeung\Locales\LocalesServiceProvider" — generates config/locales.phpconfig/locales.php, e.g.:
'locales' => [
'en' => ['name' => 'English', 'script' => 'Latn', 'native' => 'English'],
'fr' => ['name' => 'French', 'script' => 'Latn', 'native' => 'Français'],
],
app/Http/Kernel.php, add 'locale' => \ChinLeung\Locales\Middleware\SetLocale::class to $routeMiddlewarelocale middleware and use route('home') — the package automatically prepends the current locale (if prefix mode is enabled).->middleware('locale') on grouped routes; the package detects locale from URL segment (/fr/home → fr) or session/app locale.locale_url() helper in views:
<a href="{{ locale_url('home') }}">Home</a>
<a href="{{ route('home', [], locale: 'fr') }}">Français</a>
@foreach(config('locales.locales') as $code => $meta)
<a href="{{ locale_url('', $code) }}">{{ $meta['native'] }}</a>
@endforeach
\ChinLeung\Locales\Facades\Locales to get current locale, list supported locales, or detect fallback:
use ChinLeung\Locales\Facades\Locales;
$current = Locales::current(); // e.g. 'en'
$all = Locales::all(); // ['en', 'fr']
$fallback = Locales::fallback(); // 'en' (default fallback)
locale and fallback_locale; __() and trans() continue to work as expected, but now locale switching is centralized.config/locales.php. Disable via 'prefix' => false if you want cookie/session-based locale only (e.g., for dashboards). Warning: Changing this mid-project requires URL/redirect handling./de/unknown but de isn’t in config/locales.locales, the package silently falls back to default locale — ensure your middleware stack doesn’t rely on exceptions here.Locales::detectFromRequest() to trace how locale is inferred (path → session → cookie → header → default).route() caching (e.g., php artisan route:cache), locale-prefixed routes won’t auto-render with locale unless you use the helper locale_url() instead of route() for dynamic links.LocaleDetector via config/locales.php (see detector key) to add custom logic (e.g., subdomain-based: fr.example.com).<link rel="alternate" hreflang="..."> via blade partials using locale_url($path, $locale) to generate canonical hrefs.How can I help you explore Laravel packages today?