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.
Install the package via Composer:
composer require aristonis/laravel-language-switcher
Publish the configuration and migration files:
php artisan vendor:publish --provider="Aristonis\LanguageSwitcher\LanguageSwitcherServiceProvider"
Run the migrations to create the necessary tables:
php artisan migrate
Register the middleware in app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// ...
\Aristonis\LanguageSwitcher\Middleware\LanguageSwitcher::class,
],
];
Add the language switcher blade directive in your layout file:
@languageSwitcher
First Use Case: Display a language switcher dropdown in your navigation bar. The package automatically detects the user's preferred language (via browser or session) and applies it to the current request.
Language Detection:
The package detects language preference in this order: session > cookie > browser > default.
Override the default language in config/language-switcher.php:
'default' => 'en',
'supported' => ['en', 'es', 'fr'],
Manual Language Switching: Use the facade to switch languages programmatically:
use Aristonis\LanguageSwitcher\Facades\LanguageSwitcher;
LanguageSwitcher::set('es'); // Force Spanish
Route-Based Language Switching:
Prefix routes with language codes (e.g., /en/about, /es/about). Configure in config/language-switcher.php:
'prefix' => true,
Blade Directives:
@languageSwitcher: Renders the default dropdown.@languageSwitcher(['es', 'fr']): Customize supported languages for a specific view.@language: Displays the current language name (e.g., "English").Localization:
Use Laravel's built-in localization with language-specific files (e.g., resources/lang/es/messages.php). The package automatically loads the correct locale.
Dynamic Language Switching: Integrate with user authentication to persist language preferences:
// In a User model observer or controller
event(new LanguageChanged($user->language));
Listen for the event in EventServiceProvider:
protected $listen = [
\Aristonis\LanguageSwitcher\Events\LanguageChanged::class => [
\App\Listeners\UpdateUserLanguage::class,
],
];
API Integration: Use middleware to enforce language in API requests:
Route::middleware(['api', 'language.switcher'])->group(function () {
// API routes
});
Custom Views: Override the default dropdown view by publishing and modifying:
php artisan vendor:publish --tag=language-switcher.views
Middleware Order:
Ensure LanguageSwitcher middleware runs before LocaleMiddleware in app/Http/Kernel.php to avoid conflicts.
Route Caching:
Clear route cache after enabling prefix:
php artisan route:clear
Session vs. Cookie:
If using both, ensure config/language-switcher.php has consistent driver settings:
'driver' => 'cookie', // or 'session'
Locale Fallback:
The package does not handle missing translations. Always include a fallback locale in your config/app.php:
'fallback_locale' => 'en',
Database Migrations:
The package creates a language_switcher_locales table. If you customize it, back up your data before altering the schema.
Check Current Language: Use Tinker to debug:
php artisan tinker
>>> \Aristonis\LanguageSwitcher\Facades\LanguageSwitcher::getCurrentLocale();
Log Language Switches:
Enable logging in config/language-switcher.php:
'log_switches' => true,
Custom Storage:
Extend the LocaleRepository to use a custom storage backend (e.g., Redis):
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(
\Aristonis\LanguageSwitcher\Contracts\LocaleRepository::class,
\App\Repositories\CustomLocaleRepository::class
);
}
Language Detection Logic:
Override the LanguageDetector contract to implement custom logic (e.g., IP-based detection):
public function detect(): string
{
// Custom logic here
return $this->getLanguageFromIP();
}
Middleware Logic:
Extend the LanguageSwitcher middleware to add pre/post-processing:
namespace App\Http\Middleware;
use Aristonis\LanguageSwitcher\Middleware\LanguageSwitcher as BaseMiddleware;
class CustomLanguageSwitcher extends BaseMiddleware
{
public function handle($request, Closure $next)
{
// Custom logic before parent::handle()
return parent::handle($request, $next);
}
}
Register it in Kernel.php:
\App\Http\Middleware\CustomLanguageSwitcher::class,
Blade Directives:
Create custom directives by extending the LanguageSwitcherServiceProvider:
// app/Providers/LanguageSwitcherServiceProvider.php
public function boot()
{
Blade::directive('customLanguage', function () {
return "<?php echo Aristonis\LanguageSwitcher\Facades\LanguageSwitcher::getCurrentLanguageName(); ?>";
});
}
How can I help you explore Laravel packages today?