bezhansalleh/filament-language-switch
Installation
composer require bezhansalleh/filament-language-switch
Publish the config (optional, but recommended for customization):
php artisan vendor:publish --provider="BezhanSalleh\FilamentLanguageSwitch\FilamentLanguageSwitchServiceProvider"
Configure Locales
Edit config/filament-language-switch.php and define your supported locales:
'locales' => [
'en' => 'English',
'fr' => 'Français',
'de' => 'Deutsch',
],
First Use Case
Add the plugin to your Filament panel in Panel.php:
protected function getPlugins(): array
{
return [
\BezhanSalleh\FilamentLanguageSwitch\FilamentLanguageSwitchPlugin::make(),
];
}
The plugin auto-detects your panel layout (topbar, sidebar, or user menu) and renders the language switcher in the appropriate location.
Dynamic Locale Detection
The plugin automatically detects the current locale from the app() facade or the locale session variable. Override this behavior by binding a custom locale resolver:
\BezhanSalleh\FilamentLanguageSwitch\FilamentLanguageSwitch::bindLocaleResolver(function () {
return session('custom_locale_key');
});
Customizing the UI
Panel.php:
FilamentLanguageSwitchPlugin::make()
->icons([
'en' => 'heroicon-o-globe-americas',
'fr' => 'heroicon-o-globe-europe',
])
->labels([
'en' => 'English',
'fr' => 'Français (Custom)',
]),
FilamentLanguageSwitchPlugin::make()->position('sidebar'),
Integration with Filament Forms
Use the plugin’s LanguageSwitch widget in forms or tables:
use BezhanSalleh\FilamentLanguageSwitch\Widgets\LanguageSwitch;
LanguageSwitch::make()
->label('Select Language')
->required(),
Middleware for Locale Persistence
Combine with Laravel’s SetLocaleMiddleware to persist the selected locale:
// app/Http/Middleware/SetLocale.php
public function handle(Request $request, Closure $next)
{
if ($request->has('language')) {
app()->setLocale($request->language);
session(['locale' => $request->language]);
}
return $next($request);
}
Multi-Panel Support Register the plugin separately for each panel with unique configurations:
// admin/Panel.php
FilamentLanguageSwitchPlugin::make()->locales(['en', 'es']),
// tenant/Panel.php
FilamentLanguageSwitchPlugin::make()->locales(['en', 'fr']),
Locale Not Updating
SetLocaleMiddleware or manually setting the locale in a middleware/service provider:
app()->setLocale(session('locale', config('app.locale')));
Auto-Detection Fails
position or debug your panel’s layout by checking if the plugin is registered in the correct Panel class.Conflicts with Other Plugins
order option:
FilamentLanguageSwitchPlugin::make()->order(10), // Higher = appears later
Missing Translations
config/filament-language-switch.php and that the corresponding icons (e.g., Heroicons) are available in your Filament setup.Caching Issues
php artisan filament:cache-clear
\Log::info('Current locale:', ['locale' => app()->getLocale()]);
Panel class by temporarily adding:
protected function getPlugins(): array {
\Log::info('Plugins:', array_keys(class_exists(\BezhanSalleh\FilamentLanguageSwitch\FilamentLanguageSwitchPlugin::class)
? \BezhanSalleh\FilamentLanguageSwitch\FilamentLanguageSwitchPlugin::make()->getName()
: []));
return [...];
}
php artisan vendor:publish --tag="filament-language-switch-views"
Then modify resources/views/vendor/filament-language-switch/....Custom Locale Logic
Extend the LocaleResolver contract to implement custom logic (e.g., user-preferred language from a database):
use BezhanSalleh\FilamentLanguageSwitch\Contracts\LocaleResolver;
class DatabaseLocaleResolver implements LocaleResolver {
public function get(): string {
return User::find(auth()->id())->preferred_locale ?? config('app.locale');
}
}
Bind it in a service provider:
FilamentLanguageSwitch::bindLocaleResolver(new DatabaseLocaleResolver());
Add Flags or Custom Icons Dynamically generate flags or icons based on locale:
FilamentLanguageSwitchPlugin::make()
->icons(function (string $locale) {
return match ($locale) {
'en' => 'heroicon-o-globe-americas',
'fr' => 'flag-fr', // Use a custom icon class
default => 'heroicon-o-globe-europe',
};
}),
Conditional Rendering Hide the language switcher for specific users or roles:
FilamentLanguageSwitchPlugin::make()
->visible(fn () => auth()->user()->can('switch-languages')),
API Integration Expose the current locale via an API endpoint:
Route::get('/api/locale', function () {
return response()->json(['locale' => app()->getLocale()]);
});
How can I help you explore Laravel packages today?