laravel-tipi / filament-localization
Filament integration for laravel-tipi/localization
Package info
github.com/laravel-tipi/filament-localization
pkg:composer/laravel-tipi/filament-localization
Requires
- php: ^8.5
- filament/filament: ^5.10
- laravel-tipi/localization: ^0.3.0
- laravel-tipi/support: ^1.0.12
- spatie/laravel-package-tools: ^1.93
- stijnvanouplines/blade-country-flags: ^1.0.9
Requires (Dev)
- laravel-tipi/db-schema: ^0.2.0
- laravel/pint: ^1.32
- orchestra/testbench: ^11.3
- pestphp/pest: ^5.3
- pestphp/pest-plugin-laravel: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Filament integration for laravel-tipi/localization.
- Runs each Filament panel in a locale context, so each panel has its own supported locales, default and remembered choice.
- Adds a locale switcher that keeps the current page and works with Livewire requests.
- Adds management UIs for database-backed locales and locale contexts. They are thin layers over Localization's actions, rules and policies.
Locale resolution, negotiation, preferences, validation, invariants and storage (drivers, models, tables, connections, schemas) all live in laravel-tipi/localization. This package presents them in Filament and has no configuration file of its own: everything that belongs to a panel is configured on the panel's plugin.
Requirements
- PHP 8.5+
- Laravel 13
- Filament 5.10+
laravel-tipi/localization0.3+
Installation
composer require laravel-tipi/filament-localization
Laravel discovers the service provider automatically. Configure locales and contexts in config/tipi/localization.php (see the Localization documentation), then register the plugin on your panel:
use Tipi\Localization\Filament\FilamentLocalizationPlugin; public function panel(Panel $panel): Panel { return $panel // ... ->plugins([ FilamentLocalizationPlugin::make() ->context('admin'), ]); }
Locale contexts
A panel runs in one locale context. Without ->context(), the plugin uses Localization's default context (tipi.localization.contexts.default, usually web, which is shared with your public site).
Give an admin panel its own context so that it can offer different locales and remember its own choice:
// config/tipi/localization.php 'contexts' => [ 'definitions' => [ 'web' => ['locales' => '*'], 'admin' => ['locales' => ['en', 'ka'], 'default' => 'en'], ], ],
FilamentLocalizationPlugin::make()->context('admin');
For every panel request, including Livewire updates, the plugin:
- makes the context current (Localization's
UseLocaleContextmiddleware); - sets the application locale to the visitor's remembered choice for that context, then the best
Accept-Languagematch, then the context default; - adds
Vary: Accept-Language, Cookieto the response.
Filament reads the application locale, so <html lang>, Filament's own translations and its text direction follow automatically.
Multiple panels
Each panel registers its own plugin instance, and every option belongs to that panel: context, switcher, management UIs and navigation group.
// AdminPanelProvider FilamentLocalizationPlugin::make()->context('admin'); // PartnerPanelProvider: switching only FilamentLocalizationPlugin::make()->context('partners')->management(false); // OpsPanelProvider: locales only, under "Settings", without a switcher FilamentLocalizationPlugin::make() ->switcher(false) ->contextManagement(false) ->navigationGroup('Settings');
Choices are remembered per context. A choice made on the public site never changes the admin panel, and the reverse also holds.
Locale switcher
By default, the switcher is rendered before the user menu. It lists the context's supported locales in display order, shows each locale's native name and marks the current one. It renders nothing when the context offers only one locale.
Choosing a locale calls Localization::select($code, $context), which remembers the choice in the context's preference cookie and applies it. The page is then reloaded from the browser's own URL, so the current page, query string (table filters, search, pagination) and fragment are kept. Locales outside the context are rejected with a notification.
use Filament\View\PanelsRenderHook; FilamentLocalizationPlugin::make() ->showFlags(false) // language icon instead of country flags ->renderHook(PanelsRenderHook::SIDEBAR_NAV_START) // move it ->switcher(false); // or omit it
Flags use the locale's country_code and blade-country-flags. A locale without a country code, or without a matching flag, gets a generic icon.
The switcher is a Livewire component, so you can also render it yourself:
<livewire:tipi-filament-localization-locale-switcher context="admin" :show-flags="false" />
Locale and context management
The plugin registers two management UIs. Each one is available when Localization stores its records in the database:
| Localization setting | UI | Path |
|---|---|---|
locales.driver => 'database' |
LocaleResource |
/{panel}/locales |
contexts.driver => 'database' |
ManageLocaleContexts page |
/{panel}/locale-contexts |
With the config driver, locales or contexts are edited in config/tipi/localization.php, so the UI is not listed in the navigation and its page is forbidden. The drivers are independent: any of the four combinations works.
Availability is checked when a UI is accessed, not when the plugin is registered. Filament registers plugins on every request and console command, and registering this plugin reads nothing of Localization.
Locales: create (inactive unless "Active" is checked), edit, activate, deactivate, make default, delete, reorder by drag and drop, and filter by active or inactive. Form fields use Localization's LocaleRules, so errors appear on the fields.
Contexts: lists every context, including contexts declared in config that have no database row yet ("Declared in config"). Declared contexts can be edited (Localization then stores them), but not deleted. Choosing "All active locales" maps to *. New contexts start with the global default locale selected, and the default select only offers the chosen active locales.
Every operation calls the matching Localization action (CreateLocale, ReorderLocales, UpdateLocaleContext, …), and Localization enforces every rule. When it rejects an operation, for example a context without the global default locale, deactivating a locale that is a context default, or a locale code that collides with a route segment, its message is shown as a notification and the form stays open. Configuration errors are not caught.
Database
The UIs use the models configured in tipi.localization.database.models, so custom models, tables and connections need no configuration here. Localization's tables are declared with DB Schema: create them with php artisan schema:generate and php artisan migrate. This package declares no tables and publishes no migrations.
Authorization
The UIs use the policies of the configured models: Localization's LocalePolicy and LocaleContextPolicy unless you replace them. By default they deny everything until you grant the manage-localization ability:
use Illuminate\Support\Facades\Gate; Gate::define('manage-localization', fn (User $user): bool => $user->is_admin);
Each action checks its own ability: viewAny, create, update, activate, deactivate, makeDefault, reorder and delete for locales; viewAny, create, update and delete for contexts. Context policies receive the stored row, or an unsaved model for a declared context without a row. Filament checks a page's access (the user and the storage driver) on every Livewire request and re-checks each action's authorization and visibility when it runs, so a request written by hand cannot reach an action the UI does not offer. Localization's actions also refuse to write with the config driver. Replace a policy with Gate::policy():
Gate::policy(\Tipi\Localization\Models\Locale::class, \App\Policies\LocalePolicy::class); Gate::policy(\App\Models\Locale::class, \App\Policies\LocalePolicy::class); // for a custom model
Options
FilamentLocalizationPlugin::make() ->management(false) // no management UIs on this panel ->localeManagement(false) // only hide the locales resource ->contextManagement(false) // only hide the contexts page ->navigationGroup('Settings'); // default: "Localization"; null for none
Customizing actions
The management actions are classes, so Filament's configureUsing() changes them everywhere they are used:
use Tipi\Localization\Filament\Actions\DeleteLocaleAction; DeleteLocaleAction::configureUsing(fn (DeleteLocaleAction $action) => $action->modalDescription('Translations in this locale stay in the database.'));
Locales: CreateLocaleAction, EditLocaleAction, ActivateLocaleAction, DeactivateLocaleAction, MakeLocaleDefaultAction, DeleteLocaleAction. Contexts: CreateLocaleContextAction, EditLocaleContextAction, DeleteLocaleContextAction.
Translations
The package UI is translatable. Publish the language files to customize them or add languages:
php artisan vendor:publish --tag=tipi-filament-localization-translations php artisan vendor:publish --tag=tipi-filament-localization-views
How it works
- Middleware order. Filament registers a plugin's middleware when
->plugins()is called, which can come before the panel's->middleware([...]). The preference cookie is encrypted, so the context middleware must run afterEncryptCookies. The service provider adds a middleware priority entry for this, so the order of the calls in your panel provider doesn't matter. - Livewire requests. Livewire re-runs "persistent" middleware for component updates, but compares them by class name without parameters. The plugin therefore registers the bare
UseLocaleContextclass as persistent, soUseLocaleContext:adminruns again for every Livewire request on the panel. - No session state. Preferences are cookies managed by Localization, so they survive login and logout. They are per browser, not per user.
- Nothing outside panels. Requests outside Filament panels load and query nothing of this package or of Localization on its behalf. Inside a panel, the locales and contexts are loaded once per request (by the context middleware) and shared by the switcher and the management UIs, whose tables run a constant number of queries.
Testing
composer check # pint --test + pest
The tests use SQLite in memory by default. Localization's tables are created the way an application creates them: migrations generated by DB Schema, then migrate. To run them on a server, create the database and a second one named {database}_secondary (custom models use a second connection), then:
FILAMENT_LOCALIZATION_TEST_DB=mysql FILAMENT_LOCALIZATION_TEST_DB_PORT=3306 FILAMENT_LOCALIZATION_TEST_DB_PASSWORD=secret vendor/bin/pest FILAMENT_LOCALIZATION_TEST_DB=pgsql FILAMENT_LOCALIZATION_TEST_DB_PORT=5432 FILAMENT_LOCALIZATION_TEST_DB_PASSWORD=secret vendor/bin/pest
_HOST, _DATABASE (default tipi_filament_localization) and _USERNAME are also read. An unreachable server fails the tests; nothing is skipped. SQLite 3.37, the oldest supported release, runs in Docker:
docker build -t tipi-filament-localization-sqlite337 tests/Support/sqlite337
docker run --rm -v "$PWD":/app tipi-filament-localization-sqlite337 php vendor/bin/pest
CI runs SQLite, SQLite 3.37, MySQL 8.4 and PostgreSQL 15 and 18.
License
MIT.