aristonis/laravel-language-switcher
Auto-detect and switch your Laravel app locale using browser headers, session, or custom detectors. Includes middleware for per-request locale setting, session persistence, optional user profile integration, and a Blade component plus controller route for manual language selection.
Architecture Fit
The laravel-language-switcher package (v1.0) is a lightweight, middleware-based solution for handling multi-language support in Laravel applications. It aligns well with Laravel’s middleware stack and service container, making it a natural fit for applications requiring locale switching via URL parameters (e.g., /en/about, /fr/about). The package leverages Laravel’s built-in localization features (e.g., App::setLocale()), reducing custom boilerplate. Its stateless design (no database dependencies) ensures minimal overhead for simple use cases, though complex routing or session-based switching may require extensions.
Integration Feasibility Integration is straightforward for greenfield Laravel projects (v7.0+). Key dependencies include:
For legacy systems, the package’s minimal API surface (e.g., LanguageSwitcher::setLocale()) lowers the barrier to adoption. However, projects using custom locale resolution logic (e.g., cookie/session-based) may need to refactor or extend the package.
Technical Risk
AppServiceProvider::boot() for locale switching, conflicts may arise. Mitigate by evaluating middleware priority ($middlewarePriority).php artisan route:cache). Requires explicit route regeneration post-integration.Key Questions
/about vs. /en/about)?en if fr is unavailable) currently managed?Stack Fit
illuminate/support (already included in Laravel), symfony/routing (for advanced route handling).spatie/laravel-translatable for multi-language models, though this is out-of-scope for the core package.Migration Path
config/app.php, custom middleware).composer require aristonis/laravel-language-switcher.app/Http/Kernel.php (priority: web group, after TrimStrings, before VerifyCsrfToken).config/app.php and publish the package config (php artisan vendor:publish --provider="Aristonis\LanguageSwitcher\LanguageSwitcherServiceProvider").Route::prefix('{locale}')->group(...))./fr redirects to /fr/about).Compatibility
App::setLocale() or custom locale middleware. Resolve via middleware priority or composition.Sequencing
spatie/laravel-translatable).Maintenance
Support
Kernel.php and route definitions). Log locale-switching events for observability.Scaling
route:cache) is supported but requires manual regeneration during deployments.Failure Modes
| Scenario | Impact | Mitigation |
|---|---|---|
| Middleware misconfiguration | 404 errors or wrong locales | Test in staging; use php artisan route:list to debug. |
| Unsupported locale | Fallback to default locale | Configure fallback_locale in config. |
| Route cache conflicts | Broken locale-aware routes | Disable caching during development; regenerate post-deploy. |
| Database/session failures | Session-based switching breaks | Use URL-based fallback or Redis failover. |
Ramp-Up
php artisan route:list to verify locale routes.App::setLocale() in controllers).How can I help you explore Laravel packages today?