PHP code example of rodrigofs / filament-smart-translate

1. Go to this page and download the library: Download rodrigofs/filament-smart-translate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rodrigofs / filament-smart-translate example snippets


// config/app.php
'locale' => 'pt_BR', // or your preferred locale

// Resources

use Rodrigofs\FilamentSmartTranslate\Traits\Resource\ResourceTranslateble;

class UserResource extends Resource
{
    use ResourceTranslateble; // ⭐ Add this line
}

// Pages
use Rodrigofs\FilamentSmartTranslate\Traits\Page\PageTranslateble;

class Settings extends Page
{
    use PageTranslateble; // ⭐ Add this line
}

// Clusters
use Rodrigofs\FilamentSmartTranslate\Traits\Cluster\ClusterTranslateble;

class UserManagement extends Cluster
{
    use ClusterTranslateble; // ⭐ Add this line
}

// app/Providers/Filament/AdminPanelProvider.php
use Rodrigofs\FilamentSmartTranslate\TranslationPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ... other configurations
        ->plugins([
            TranslationPlugin::make(), // ⭐ Add this line
            // ... other plugins
        ]);
}

// config/filament-smart-translate.php
'translation_page' => [
    'enabled' => true,
    'dev_only' => true, // ⭐ Important: Only in development
    'navigation' => [
        'enabled' => true,
        'group' => 'Development',
        'sort' => 99,
        'icon' => 'heroicon-o-language',
    ],
    'page' => [
        'title' => 'Translation Manager',
        'navigation_label' => 'Translations',
    ],
    'features' => [
        'export' => true,
        'backup' => true,
        'language_selector' => true,
        'bulk_actions' => true,
    ],
    'authorize' => env('APP_DEBUG', false), // Only when debug is enabled
],

'translation_page' => [
    'enabled' => env('TRANSLATION_MANAGER_ENABLED', false),
    'dev_only' => false,
    'authorize' => function () {
        return auth()->user()?->hasRole('admin');
    },
],

'components' => [
    'fields' => [
        'enabled' => true,
        'fallback_strategy' => 'original'
    ],
    'resources' => [
        'enabled' => true,
        'fallback_strategy' => 'humanize'
    ],
    'actions' => [
        'enabled' => true,
        'fallback_strategy' => 'lower_case'
    ]
]



namespace App\Strategies;

use Rodrigofs\FilamentSmartTranslate\Support\Fallback\FallbackStrategyInterface;

class UppercaseStrategy implements FallbackStrategyInterface
{
    public function apply(string $key): string
    {
        return strtoupper(str_replace('_', ' ', $key));
    }
}

'fallback_strategies' => [
    'uppercase' => \App\Strategies\UppercaseStrategy::class,
],



namespace App\Filament\Resources;

use App\Models\User;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Tables;
use Rodrigofs\FilamentSmartTranslate\Traits\Resource\ResourceTranslateble;

class UserResource extends Resource
{
    use ResourceTranslateble; // ⭐ Add this for model labels

    protected static ?string $model = User::class;
    protected static ?string $navigationGroup = 'user_management';

    public static function form(Form $form): Form
    {
        return $form->schema([
            // These translate automatically (no code changes needed)
            Forms\Components\TextInput::make('name'),
            Forms\Components\TextInput::make('email'),
            Forms\Components\Select::make('role'),

            Forms\Components\Section::make('profile') // Section titles too!
                ->schema([
                    Forms\Components\TextInput::make('first_name'),
                    Forms\Components\TextInput::make('last_name'),
                ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                // Column headers automatically translated
                Tables\Columns\TextColumn::make('name'),
                Tables\Columns\TextColumn::make('email'),
                Tables\Columns\TextColumn::make('role'),
            ])
            ->actions([
                // Action labels automatically translated
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ]);
    }
}


// config/filament-smart-translate.php

return [
    // Global enable/disable
    'enabled' => env('FILAMENT_SMART_TRANSLATE_ENABLED', true),

    // Translation Manager (Development Feature)
    'translation_page' => [
        'enabled' => false, // Enable translation management interface
        'dev_only' => true, // ⚠️ Important: Only in development
        'navigation' => [
            'enabled' => true,
            'group' => 'Development',
            'sort' => 99,
            'icon' => 'heroicon-o-language',
        ],
        'page' => [
            'title' => 'Translation Manager',
            'navigation_label' => 'Translations',
        ],
        'features' => [
            'export' => true,
            'backup' => true,
            'language_selector' => true,
            'bulk_actions' => true,
        ],
        'authorize' => env('APP_DEBUG', false),
    ],

    // Component-specific settings
    'components' => [
        'resources' => [
            'enabled' => true,
            'fallback_strategy' => 'original'
        ],
        'fields' => [
            'enabled' => true,
            'fallback_strategy' => 'original'
        ],
        'actions' => [
            'enabled' => true,
            'fallback_strategy' => 'original'
        ],
        // ... other components
    ],

    // Custom fallback strategies
    'fallback_strategies' => [
        // 'custom' => \App\Strategies\CustomStrategy::class,
    ],

    // Debug settings
    'debug' => [
        'log_missing_translations' => env('FILAMENT_SMART_TRANSLATE_DEBUG', false),
    ],
];


// Resource
use ResourceTranslateble;

// Page
use PageTranslateble;

// Cluster
use ClusterTranslateble;

bash
php artisan vendor:publish --tag=filament-smart-translate-config