PHP code example of dyahunter35 / filament-translation-toolkit

1. Go to this page and download the library: Download dyahunter35/filament-translation-toolkit 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/ */

    

dyahunter35 / filament-translation-toolkit example snippets


'locales' => ['en', 'ar'],  // First locale = base language

// app/Providers/Filament/AdminPanelProvider.php

use Dyahunter35\FilamentTranslationToolkit\Pages\TranslationDashboard;

public function panel(Panel $panel): Panel
{
    return $panel
        // ... other config
        ->pages([
            TranslationDashboard::class,
        ]);
}

// config/filament-translation-toolkit.php
'navigation' => [
    'label' => 'My Dashboard',           // null = use __() translations
    'group' => 'Admin Tools',            // null = use __() translations
    'title' => 'Translation Overview',   // null = use __() translations
    'icon'  => 'heroicon-o-cog-6-tooth',
    'sort'  => 100,
],

// config/filament-translation-toolkit.php

return [

    // Master switch — disable all translation globally
    'enabled' => env('FILAMENT_TRANSLATION_ENABLED', true),

    // Locales to generate translations for (first = base)
    'locales' => ['en', 'ar'],

    // Eloquent models namespace (for relationship detection)
    'model_namespace' => 'App\\Models',

    // Translation files directory (null = base_path('lang'))
    'lang_path' => null,

    // Default Heroicon for generated files
    'default_icon' => 'heroicon-m-building-office-2',

    // Use Resource defaults (label, group, icon) from Filament Resource classes
    'use_resource_defaults' => env('TRANSLATION_USE_RESOURCE_DEFAULTS', true),

    // AI Translation Service
    'ai' => [
        'api_key'   => env('OPENROUTER_API_KEY', ''),
        'model'     => env('OPENROUTER_MODEL', 'openai/gpt-4o-mini'),
        'endpoint'  => 'https://openrouter.ai/api/v1/chat/completions',
        'timeout'   => 45,
    ],

    // Which keys to ,
        'icon'  => env('TRANSLATION_DASHBOARD_ICON', 'heroicon-o-language'),
        'sort'  => (int) env('TRANSLATION_DASHBOARD_SORT', 999),
    ],

];

namespace App\Models;

use Dyahunter35\FilamentTranslationToolkit\Concerns\Translatable;
use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    use Translatable;

    // Optional: customize the translation file name
    // public static ?string $translationFileName = 'company';

    // Optional: disable translation for this model
    // public static bool $translationEnabled = false;
}

$model->getTranslationFileName();  // Get the translation file name
Company::getTranslationFileBasename();  // Get the base file name (snake_case)
Company::isTranslatable();  // Check if translation is enabled

namespace App\Filament\Resources;

use Dyahunter35\FilamentTranslationToolkit\Concerns\HasResource;
use Filament\Resources\Resource;

class CompanyResource extends Resource
{
    use HasResource;

    public static function getModel(): string
    {
        return \App\Models\Company::class;
    }

    public static function form(array $form): array
    {
        return $form->schema([
            // ... your form fields — labels auto-translated!
        ]);
    }
}

namespace App\Filament\Resources\CompanyResource\RelationManagers;

use Dyahunter35\FilamentTranslationToolkit\Concerns\HasRelationManager;
use Filament\Resources\RelationManagers\RelationManager;

class DocumentsRelationManager extends RelationManager
{
    use HasRelationManager;

    protected static string $relationship = 'documents';
}

class InternalDashboard extends Resource
{
    use HasResource;

    public static bool $translationEnabled = false; // disables translation

    // ...
}

'enabled' => false,

class CompanyResource extends Resource
{
    use HasResource;

    public static function boot(): void
    {
        static::bootTranslation(); // calls all 3 configure methods
    }
}

use Dyahunter35\FilamentTranslationToolkit\Concerns\Translatable;

class Company extends Model
{
    use Translatable;

    // Optional: customize the translation file name
    public static ?string $translationFileName = 'company_info';

    // Optional: disable translation for this specific model
    public static bool $translationEnabled = false;
}

$model->getTranslationFileName();      // 'company_info'
Company::getTranslationFileBasename(); // 'company'
Company::isTranslatable();             // true/false

// Auto-boot all translation callbacks (optional — global auto-registration handles this)
static::bootTranslation();

// Or call individually:
static::translateConfigureForm();     // Auto-translate form fields
static::translateConfigureTable();    // Auto-translate table columns
static::translateConfigureInfolist(); // Auto-translate infolist entries

// Disable translation for this specific class:
public static bool $translationEnabled = false;

// Check if translation is enabled:
if (static::shouldTranslate()) { ... }

// Extract all translatable strings from a form
$strings = static::extractTranslatableStrings($form);

// Create a multi-language form component
$component = static::multiLanguageFormComponent($field, ['en', 'ar']);

class CompanyResource extends Resource
{
    use HasResource;

    // Automatically translates:
    // - getNavigationLabel()
    // - getNavigationGroup()
    // - getNavigationIcon()
    // - getModelLabel()
    // - getPluralModelLabel()
    // - getBreadcrumb()
    // - getGlobalSearchResultUrl()
}

public static ?string $localePath = 'custom_company';

class ExpenseSettings extends Page
{
    use HasSinglePage;

    protected string $view = 'filament.pages.expense-settings';

    // Automatically translates:
    // - getNavigationLabel()
    // - getNavigationGroup()
    // - getActiveNavigationIcon()
    // - getHeading()
    // - getSubheading()
    // - getModelLabel()
    // - getTitle()
}

class EditCompany extends EditRecord
{
    use HasResourcePage;

    // Locale path comes from the parent Resource
    // Automatically translates navigation label and title
}

class DocumentsRelationManager extends RelationManager
{
    use HasRelationManager;

    protected static string $relationship = 'documents';

    // Locale path: documents_relation
    // Automatically translates:
    // - getTitle()
    // - getPluralRecordLabel()
    // - getRecordLabel()
}

class CompanyReport extends Page
{
    use HasPage;

    protected static string $resource = CompanyResource::class;

    // Locale path comes from the parent Resource's model
}

// lang/en/company.php

return [
    'navigation' => [
        'group' => 'Companies',
        'label' => 'Companies',
        'plural_label' => 'Companies',
        'model_label' => 'Company',
        'icon' => 'heroicon-m-building-office-2',
    ],
    'breadcrumbs' => [
        'index' => 'Companies',
        'create' => 'Add Company',
        'edit' => 'Edit Company',
    ],
    'sections' => [
        'company_details' => [
            'label' => 'Company Details',
            'description' => 'Basic company information',
            'icon' => 'heroicon-o-building-office',
        ],
    ],
    'fields' => [
        'name' => [
            'label' => 'Company Name',
            'placeholder' => 'Enter company name',
            'helper_text' => 'The legal name of the company',
            'prefix' => '',
            'suffix' => '',
            'description' => '',
            'icon' => '',
        ],
        'status' => [
            'label' => 'Status',
            'options' => [
                'active' => 'Active',
                'inactive' => 'Inactive',
            ],
        ],
    ],
    'tabs' => [
        'details' => [
            'label' => 'Details',
            'description' => 'Basic information',
            'icon' => 'heroicon-o-information-circle',
        ],
    ],
    'filters' => [
        'status' => ['label' => 'Filter by Status'],
    ],
    'actions' => [
        'edit' => 'Edit',
        'delete' => 'Delete',
    ],
];

// lang/en/document_relation.php

return [
    'label' => [
        'plural' => 'Documents',
        'single' => 'Document',
    ],
    'fields' => [
        'title' => [
            'label' => 'Document Title',
            'placeholder' => 'Enter document title',
        ],
        'uploaded_at' => [
            'label' => 'Uploaded At',
        ],
    ],
    'filters' => [
        'created_at' => [
            'label' => 'Filter by Created At',
        ],
    ],
    'actions' => [
        'edit' => 'Edit',
        'delete' => 'Delete',
    ],
];

// config/filament-translation-toolkit.php
'ai' => [
    'endpoint' => 'https://your-api-endpoint.com/v1/chat/completions',
    'model' => 'your-model-name',
    'api_key' => env('YOUR_API_KEY', ''),
],



namespace Dyahunter35\FilamentTranslationToolkit\Templates;

class WidgetTemplate extends BaseTranslationTemplate
{
    public function getJsonStructure(): string
    {
        return '
        {
            "en": {
                "title": "Widget Title",
                "description": "Widget description",
                "fields": { "stat1": { "label": "Stat 1" } }
            },
            "ar": { ... }
        }';
    }

    public function build(array $langData): string
    {
        $content = "    'title' => '{$this->escape($langData['title'] ?? '')}',\n";
        $content .= "    'description' => '{$this->escape($langData['description'] ?? '')}',\n";

        $content .= "    'fields' => [\n";
        foreach ($langData['fields'] ?? [] as $key => $field) {
            $label = is_array($field) ? ($field['label'] ?? $key) : $field;
            $content .= "        '{$this->escape((string) $key)}' => [\n";
            $content .= "            'label' => '{$this->escape($label)}',\n";
            $content .= "        ],\n";
        }
        $content .= "    ],\n";

        return $content;
    }
}
bash
php artisan vendor:publish --tag=filament-translation-toolkit-config
bash
php artisan make:translation widgets --ai --type=widget