craft-forge/filament-language-switcher
Installation:
composer require craft-forge/filament-language-switcher
Ensure your composer.json meets the package's PHP 8.1+ and Filament 3.x/4.x/5.x requirements.
Register the Plugin:
Add the plugin to your AdminPanelProvider (or other panel provider):
use CraftForge\FilamentLanguageSwitcher\FilamentLanguageSwitcherPlugin;
public function panel(Panel $panel): Panel {
return $panel->plugins([
FilamentLanguageSwitcherPlugin::make(),
]);
}
First Use Case:
resources/lang/) and renders a dropdown with flags in the Filament navbar.Auto-Detection:
resources/lang/ for .json files (e.g., en.json, fr.json) and maps them to locales.FilamentLanguageSwitcherPlugin::make()
->locales(['en', 'fr', 'de'])
Customization:
flags() to map locales to custom flag icons (e.g., ['en' => '🇬🇧']).labels():
->labels(['en' => 'English', 'fr' => 'Français'])
position() (e.g., 'end' for navbar right side).Persistence:
persistViaCookies(true) (requires session() config in config/filament.php).Integration with Filament Features:
LocaleChanged events for post-switch logic:
event(new FilamentLanguageSwitcher\Events\LocaleChanged($newLocale));
Multi-Panel Support:
Dynamic Locale Loading:
->locales(fn () => Locale::query()->pluck('code')->toArray())
Conditional Rendering:
->visible(fn () => auth()->user()->isAdmin())
Localization Fallbacks:
spatie/laravel-translatable for fallback chains:
->fallbackLocale('en')
Testing:
$this->partialMock(FilamentLanguageSwitcherPlugin::class, function ($mock) {
$mock->shouldReceive('getLocales')->andReturn(['en', 'es']);
});
Locale File Naming:
en_US.json → en_us.json). Rename files if auto-detection fails.Caching Issues:
php artisan filament:cache-clear) after adding new locales or flags.Session/Cookie Conflicts:
persistViaCookies(true), ensure session() is configured in config/filament.php:
'session' => [
'driver' => 'database', // or 'cookie', 'redis', etc.
],
Dark Mode Flags:
🇫🇷 instead of 🇫🇷) or SVG icons for better contrast.Locale Code Mismatches:
locales() match those in your translation files (e.g., en_US vs. en).Log Auto-Detected Locales:
Add this to your AppServiceProvider to debug:
public function boot() {
\Log::info('Detected locales:', [
'locales' => app(FilamentLanguageSwitcherPlugin::class)->getLocales(),
]);
}
Check Event Firing:
Listen for LocaleChanged events to verify triggers:
event(new \CraftForge\FilamentLanguageSwitcher\Events\LocaleChanged($locale));
Override Blade Views: Customize the dropdown template by publishing views:
php artisan vendor:publish --tag="filament-language-switcher-views"
Then modify resources/views/vendor/filament-language-switcher/....
Custom Storage: Extend persistence logic by binding a custom locale resolver:
$this->app->bind(\CraftForge\FilamentLanguageSwitcher\Contracts\LocaleResolver::class, function () {
return new CustomResolver();
});
API Integration: Sync locales with a headless CMS (e.g., Strapi) via a scheduled job:
public function handle() {
$locales = $this->fetchLocalesFromStrapi();
app(FilamentLanguageSwitcherPlugin::class)->locales($locales);
}
Localization Providers:
Integrate with laravel-localization or mcamara/laravel-localization for advanced features like:
/en/page).Testing Helpers: Create a trait for testing locale switches:
trait TestsLocaleSwitching {
protected function switchLocale(string $locale) {
$this->actingAsUser(factory(User::class)->create());
$this->post('/filament-language-switcher/change', ['locale' => $locale]);
}
}
How can I help you explore Laravel packages today?