mominalzaraa/filament-localization
Installation
composer require mominalzaraa/filament-localization
Publish the config and migrations:
php artisan vendor:publish --provider="MominAlZaraa\FilamentLocalization\FilamentLocalizationServiceProvider" --tag="config"
php artisan vendor:publish --provider="MominAlZaraa\FilamentLocalization\FilamentLocalizationServiceProvider" --tag="migrations"
php artisan migrate
Configure Supported Locales
Update config/filament-localization.php:
'locales' => [
'en' => 'English',
'ar' => 'Arabic',
// Add more as needed
],
First Use Case: Localize a Filament Resource Run the scanner to auto-generate translations:
php artisan filament-localization:scan
This scans your Filament resources (e.g., app/Filament/Resources/) and generates structured translation files in resources/lang/.
config/filament-localization.php (adjust locales, paths, and behavior).resources/lang/{locale}/filament-localization.php (auto-generated).php artisan filament-localization:scan (re-run after adding new resources).app/Http/Middleware/SetLocale.php (if custom locale logic is needed).Develop Filament Resources
Create a resource (e.g., php artisan make:filament-resource Post).
Add translatable strings (e.g., labels, buttons, validation messages) using Filament’s built-in localization helpers:
Title::make('name')->label(__('filament-localization::resources.posts.fields.name.label'));
Scan and Generate Translations Run the scanner to extract strings and generate translation files:
php artisan filament-localization:scan
The scanner respects:
filament-localization::resources.posts.fields.name.label).Translate Strings
Edit the generated files (e.g., resources/lang/ar/filament-localization.php) and add translations:
return [
'resources' => [
'posts' => [
'fields' => [
'name' => [
'label' => 'الاسم',
],
],
],
],
];
Integrate Locale Switching Use the provided middleware or Filament’s built-in locale selector:
// In FilamentPanelProvider
$panel->middleware([
SetLocale::class, // Auto-detects or uses session locale
]);
Custom Scanning Paths Extend the scanner to include non-standard paths (e.g., plugins):
// config/filament-localization.php
'scan_paths' => [
app_path('Filament/Resources'),
base_path('packages/custom-plugin/resources/filament'),
],
Dynamic Translation Keys Use dynamic keys for reusable components:
// In a Filament component
__("filament-localization::components.{component_name}.{key}", ['component_name' => 'user-card']);
Fallback Locales
Configure fallback locales in config/filament-localization.php:
'fallback_locale' => 'en',
'fallback_locale_hierarchy' => ['en', 'ar'],
Testing Localization Use the package’s testing helpers in PHPUnit:
use MominAlZaraa\FilamentLocalization\Facades\FilamentLocalization;
public function test_translations()
{
FilamentLocalization::setLocale('ar');
$this->assertEquals('الاسم', __('filament-localization::resources.posts.fields.name.label'));
}
__() or |trans:
TextInput::make('bio')->label(__('filament-localization::resources.users.fields.bio.label'));
<x-filament::section.heading>{{ __('filament-localization::resources.posts.heading') }}</x-filament::section.heading>
return response()->json(['error' => __('filament-localization::errors.validation')], 422);
filament-localization.php file:
// resources/lang/ar/filament-localization.php
return [
'packages' => [
'spatie-laravel-permission' => [
'labels' => [
'role' => 'دور',
],
],
],
];
Scanner Not Picking Up New Resources
filament-localization:scan). Forgetting to re-run after adding new resources.php artisan filament-localization:scan --force
Translation Keys Not Found
__() or missing translation files.filament-localization::resources.posts.fields.name.label).resources/lang/ar/filament-localization.php).--debug flag to see scanned keys:
php artisan filament-localization:scan --debug
Locale Not Switching
SetLocale middleware is added to Filament’s panel:
$panel->middleware([SetLocale::class]);
$this->actingAsUser($user)->withSession(['locale' => 'ar']);
Overwriting Translations
scan overwrites existing translations.--dry-run to preview changes or back up files before scanning.Log Scanned Keys Enable debug mode in the config:
'debug' => true,
Or run:
php artisan filament-localization:scan --debug
Check Generated Files
Inspect resources/lang/{locale}/filament-localization.php for missing keys. If a key is missing, it wasn’t scanned—re-run the scanner.
Verify Middleware
Check if SetLocale is applied to Filament’s panel:
php artisan route:list | grep filament
Ensure the middleware is listed.
Fallback Locale Issues
If translations are missing, confirm the fallback hierarchy in config/filament-localization.php:
'fallback_locale_hierarchy' => ['en', 'ar'],
Custom Scanners Extend the scanner by creating a custom service provider:
use MominAlZaraa\FilamentLocalization\Scanners\ScannerInterface;
class CustomScanner implements ScannerInterface
{
public function scan(): array
{
// Custom logic to scan additional paths
return [];
}
}
Register it in config/filament-localization.php:
'scanners' => [
\MominAlZaraa\FilamentLocalization\Scanners\ResourceScanner::class,
App\Scanners\CustomScanner::class,
],
Dynamic Key Generation Override the key generator for dynamic keys:
// config/filament-localization.php
'key_generator' => \App\Services\CustomKeyGenerator::class;
Post-Scan Hooks Add logic after scanning via events:
// In a service provider
event(new \MominAlZaraa\FilamentLocalization\Events\ScanCompleted($scannedKeys));
Custom Translation Loaders Extend the package’s translation loader to support additional sources (e.g., databases):
use MominAlZaraa\FilamentLocalization\Loaders\TranslationLoaderInterface;
class DatabaseTranslationLoader implements TranslationLoaderInterface
{
public function load(string $locale, string $group): array
{
// Load from DB
return [];
}
}
How can I help you explore Laravel packages today?