jeffersongoncalves / laravel-locale-cookie
A Laravel middleware that resolves the application locale from a cookie, validating the requested value against a configurable list of supported locales and falling back to a sensible default when the cookie is missing or unknown. Cookie name, supported locales, and fallback locale are all driven by
Package info
github.com/jeffersongoncalves/laravel-locale-cookie
pkg:composer/jeffersongoncalves/laravel-locale-cookie
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/support: ^11.0|^12.0|^13.0
- spatie/laravel-package-tools: ^1.14
Requires (Dev)
- larastan/larastan: ^3.0
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^3.7.4|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
This package is auto-updated.
Last update: 2026-06-20 21:26:23 UTC
README
Laravel Locale Cookie
A Laravel middleware that resolves the application locale from a cookie. The requested value is validated against a configurable list of supported locales and ignored when it is missing or unknown, falling back to a sensible default. Cookie name, supported locales, and fallback locale are all driven by config.
Installation
You can install the package via composer:
composer require jeffersongoncalves/laravel-locale-cookie
You can publish the config file with:
php artisan vendor:publish --tag="locale-cookie-config"
This is the contents of the published config file:
return [ 'cookie' => 'locale', 'supported' => ['en'], 'fallback' => null, ];
cookie— the name of the cookie the middleware reads the visitor's locale from (defaultlocale).supported— the whitelist of accepted locales. A cookie value that is not in this list is ignored.fallback— the locale applied when the cookie is missing or unsupported. Whennull, the middleware falls back to the framework'sconfig('app.fallback_locale').
Usage
Register the middleware on the route group that should be locale-aware. The middleware reads the cookie, validates it against your supported list, and applies the matching locale (or the fallback) for the rest of the request:
use JeffersonGoncalves\LocaleCookie\Middleware\SetLocale; Route::middleware(SetLocale::class)->group(function () { // ...locale-aware routes });
You can also alias it in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ \JeffersonGoncalves\LocaleCookie\Middleware\SetLocale::class, ]); })
Using with Livewire
If your locale-aware pages use Livewire, register the middleware as a Livewire persistent middleware as well, so subsequent /livewire/update requests keep the visitor's locale instead of falling back to the default. Register both the route-group middleware and the persistent middleware (pattern taken from the source project this package was extracted from):
use Livewire\Livewire; use JeffersonGoncalves\LocaleCookie\Middleware\SetLocale; // In a service provider's boot() method Livewire::addPersistentMiddleware([ SetLocale::class, ]);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
