adultdate/filament-language-switch
最新稳定版本:4.0.0
Composer 安装命令:
composer require adultdate/filament-language-switch
包简介
Zero config Language Switch(Changer/Localizer) plugin for filamentphp admin
关键字:
README 文档
README
Language Switch
The Language Switch plugin is a versatile and user-friendly tool designed for Filament Panels. It offers a seamless way to integrate language switching capabilities into your Filament Panels. With a range of customizable options and a fluent API, the plugin allows you to easily configure language selection for your users. It supports displaying language options both within and outside of Filament panels, and it provides the flexibility to specify which panels or routes should include the language switch and much more.
Compatibility
| Package Version | Filament Version |
|---|---|
| v1 | v2 |
| v3 | v3 |
| v4 | v4 |
Upgrading from v3 to v4
If you are upgrading from version 3 to version 4, you will need to update the namespace anywhere you are using the plugin from BezhanSalleh\LanguageSwitch to BezhanSalleh\FilamentLanguageSwitch.
Installation
Install the package via composer:
composer require adultdate/filament-language-switch
Important
The plugin follows Filament's theming rules. So, to use the plugin create a custom theme if you haven't already, and add the following line to your theme.css file:
@source '../../../../vendor/adultdate/filament-language-switch/resources/views/**/*.blade.php';
Now build your theme using:
npm run build
Usage
The plugin boots after installation automatically. For the plugin to work, provide an array of locales that your Panel(s) support to switch between them inside a service provider's boot() method. You can either create a new service provider or use the default AppServiceProvider as follow:
... use BezhanSalleh\LanguageSwitch\LanguageSwitch; class AppServiceProvider extends ServiceProvider { ... public function boot() { ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ->locales(['ar','en','fr']); // also accepts a closure }); ... } }
Though this is all you would need, but the plugin is designed to be very customizable. Checkout the Laravel localization documentation to get started with localization. Delve into the Configuration section below for detailed customization options.
Configuration
The plugin comes with following options that you can customize and configure as per your requirements. The plugin has a fluent API so you can chain the methods and easily configure it all in one place.
Event
Whenever the locale is changed, the plugin dispatches a LocaleChanged event. You can listen to this event in your application to perform any additional actions or logic when the language is switched.
use BezhanSalleh\LanguageSwitch\Events\LocaleChanged; use Illuminate\Support\Facades\Event; /** * Bootstrap any application services. */ public function boot(): void { Event::listen(function (LocaleChanged $event) { // persist the new locale in the user's profile, or perform any other action // auth()->user()->setLocale($event->locale); }); }
Visibility Control
The visible() method configures the visibility of the Language Switch within the application's UI. It has two parameters:
-
insidePanels: Determines if the language switcher is visible inside Filament panels, which is
trueby default. The language switcher will be visible inside panels only if theinsidePanelscondition is true, more than one locale is available, and the current panel is included (not excluded). -
outsidePanels: Controls visibility outside of the panels, with a default of false. The language switcher will be visible outside of panels only if the
outsidePanelscondition is true, the current route is included in theoutsidePanelRoutes(), and the current panel is included (not excluded).
Both arguments can be provided as a boolean or a Closure that returns a boolean value. The Closure enables dynamic determination of visibility, allowing you to incorporate complex logic based on the application state, user permissions, or other criteria.
//AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->visible(outsidePanels: true) ...; }); ...
Outside Panel Routes
The outsidePanelRoutes() method is used to define the routes where the Language Switch should be visible outside of the Filament panels. This method accepts either an array of route names or a Closure that returns an array of route names. By default, it includes common authentication routes such as auth.login, auth.profile, and auth.register.
To specify custom routes for displaying the language switcher, pass an array of route names to the method:
// AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->outsidePanelRoutes([ 'profile', 'home', // Additional custom routes where the switcher should be visible outside panels ]) ...; }); ...
If you want to dynamically determine the routes, use a Closure:
// AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->outsidePanelRoutes(fn() => someCondition() ? ['dynamic.route'] : ['default.route']) ...; }); ...
Outside Panel Placement
The outsidePanelPlacement() method specifies the placement of the Language Switch when it is rendered outside of Filament panels. This method accepts an Placement enum value that determines the switch's position on the screen.
You can choose from the following placements defined in the Placement enum:
TopLeftdefaultTopCenterTopRightBottomLeftBottomCenterBottomRight
Set the desired placement for the language switch outside Filament Panels like this:
// AppServiceProvider.php ... use BezhanSalleh\LanguageSwitch\Enums\Placement; LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->outsidePanelPlacement(Placement::BottomRight) // Sets the language switch to appear at the bottom right outside of panels ...; }); ...
Localized Labels
The displayLocale() method is used to set the locale that influences how labels for given locales are generated by PHP's native function locale_get_display_name(). This method specifies the language in which the labels for given locales are displayed when custom labels are not set using the labels() method.
By default, if displayLocale() is not explicitly set, the locale labels are generated based on the application's current locale. This affects the automatic label generation for locales without custom labels.
For example, if your application's current locale is English ('en'), and you have not set a specific display locale, then the labels for locales like pt_BR and pt_PT would automatically be generated as Portuguese (Brazil) and Portuguese (Portugal) respectively, in English.
To specify a different language for the automatic label generation, use displayLocale():
// AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->displayLocale('fr') // Sets French as the language for label localization ...; }); ...
Custom Labels
The labels() method in the Language Switch allows you to define custom text labels for each locale that your application supports. This customization is particularly useful when the default labels generated by PHP's native function locale_get_display_name() are not suitable for your application's needs.
By default, if no custom labels are provided, the Language switch will generate labels for each locale using the native PHP function locale_get_display_name(), which creates a label based on the current application's locale. For example, the locales pt_BR and pt_PT will be labeled as Portuguese (Brazil) and Portuguese (Portugal) respectively, when the application's locale is set to en.
However, you might prefer to display labels that are shorter or differently formatted. This is where the labels() method is beneficial. You can specify exactly how each language should be labeled, overriding the default behavior.
Here's how to set custom labels:
// AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->locales(['en','fr','pt_BR','pt_PT']) ->labels([ 'pt_BR' => 'Português (BR)', 'pt_PT' => 'Português (PT)', // Other custom labels as needed ]) ...; }); ...
Panel Exclusion
By default the Language Switch will be available inside all existing Panels. But you can choose which panels will have the switch by providing an array of valid panel ids using the exclude() method. The method also accepts a Closure so you have more control over how to exclude certain panels.
//AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->excludes([ 'app' ]) ...; }); ...
Render Hook
By default the panels::global-search.after hook is used to render the Language Switch. But you can use any of the Render Hooks available in Filament using the renderHook() method as:
//AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->renderHook('panels::global-search.before') ...; }); ...
Flags
By default the Language Switch uses the locales as Language Badges to serve as placeholders for the flags. But you may associate each locale with its corresponding flag image by passing an array to the flags() method. Each key in the array represents the locale code, and its value should be the asset path to the flag image for that locale. For example, to set flag images for Arabic, French, and English (US), you would provide an array like this:
//AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->flags([ 'ar' => asset('flags/saudi-arabia.svg'), 'fr' => asset('flags/france.svg'), 'en' => asset('flags/usa.svg'), ]) ...; }); ...
Make sure that the provided paths in the asset() helper point to the correct location of the flag images in your Laravel project's public directory.
Flags Only
The flagsOnly() method controls whether the Language Switch displays only flag images, without accompanying text labels. This method can enhance the UI by providing a cleaner look when space is limited or when you prefer a more visual representation of language options.
To display only the flags for each language, invoke the method and make sure you have provided the flags for locales just like shown above using the flags() method:
//AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->flags([ 'ar' => asset('flags/saudi-arabia.svg'), 'fr' => asset('flags/france.svg'), 'en' => asset('flags/usa.svg'), ]) ->flagsOnly() ...; }); ...
Circular
By default the Language Switch Flags or Language Badges are slightly rounded like the most other Filament components. But you may make it fully rounded using the circular() method.
//AppServiceProvider.php ... LanguageSwitch::configureUsing(function (LanguageSwitch $switch) { $switch ... ->circular() ...; }); ...
Views
In case you want to tweak the design, you can publish the views using the following command and adjust it however you like:
php artisan vendor:publish --tag="language-switch-views"
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
If you want to contribute to this package, you may want to test it in a real Filament project:
- Fork this repository to your GitHub account.
- Create a Filament app locally.
- Clone your fork in your Filament app's root directory.
- In the
/filament-language-switchdirectory, create a branch for your fix, e.g.fix/error-message.
Install the packages in your app's composer.json:
"require": { "adultdate/filament-language-switch": "dev-fix/error-message as main-dev", }, "repositories": [ { "type": "path", "url": "filament-language-switch" } ]
Now, run composer update.
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.
adultdate/filament-language-switch 适用场景与选型建议
adultdate/filament-language-switch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「bezhanSalleh」 「filament-plugin」 「filament-language-switch」 「filament-language-changer」 「filament-locale-changer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adultdate/filament-language-switch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adultdate/filament-language-switch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adultdate/filament-language-switch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Set of Filament Component Addons with different designs and extra functionalities
A Simple & Beautiful Pluggable Exception Viewer for FilamentPHP's Admin Panel
jitone-ai is a powerful FilamentPHP plugin that integrates AI-powered features directly into your Filament forms.
Send Notification to discord channel Webhook using native FilamentPHP Notification Facade class
Filament support for `spatie/laravel-permission`.
Alfabank REST API integration
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-17