PHP code example of volcy / translator-flight

1. Go to this page and download the library: Download volcy/translator-flight 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/ */

    

volcy / translator-flight example snippets


// app/config/config.php
return [
    'translator' => [
        'index_path'      => PROJECT_ROOT . '/storage/translator/indexes',
        'views_path'      => PROJECT_ROOT . '/app/views',
        'compile_path'    => PROJECT_ROOT . '/storage/views_compiled',
        'source_locale'   => 'en', // Your original template language
        'fallback_locale' => 'en',
        'id_strategy'     => 'hash', // 'hash', 'tag_path', or 'explicit'
        'locale_resolver' => fn () => $_GET['lang'] ?? $_SESSION['locale'] ?? 'en',
        'excluded_folders' => ['/admin'], // Optional: folders to exclude from scanning
        'translation_driver' => 'groq', // 'groq', 'google', or 'cerebras'
        'drivers' => [
            'groq' => [
                'key' => $_ENV['GROQ_API_KEY'] ?? null,
                'model' => 'llama-3.1-8b-instant',
            ],
            'cerebras' => [
                'key' => $_ENV['CEREBRAS_API_KEY'] ?? null,
                'model' => 'gemma-4-31b',
            ],
        ],
    ],
];

// app/config/services/translator.php
use Volcy\Translator\Flight\TranslatorBootstrap;

$translator = TranslatorBootstrap::register($app, $config['translator']);
$app->set('translator', $translator);

// app/config/services/view.php
use Volcy\Translator\Flight\TrackedBladeOne as BladeOne;

$app->register(
    'view',
    BladeOne::class,
    [BladeOne::MODE_FAST], // Use MODE_FAST for production, MODE_DEBUG for development
    function (BladeOne $blade) use ($ds, $app, $config) {
        $views = realpath(PROJECT_ROOT . '/app/views');
        $cache = realpath(PROJECT_ROOT . '/storage/views_compiled');
        $blade->setPath($views, $cache);
        $blade->setRegistry($app->get('translator')->registry());
    }
);

// Create a custom render function that uses the translator-aware BladeOne
$app->map(
    'render',
    function (string $template, array $data = []) use ($app, $config): void {
        $blade = $app->get('translator')->blade(
            $config['translator']['views_path'],
            $config['translator']['compile_path']
        );
        echo $blade->run($template, $data);
    }
);

// app/config/routes/web.php
Flight::group('/', function () {
    // Your routes here
}, [$app->get('translator')->middleware()]);

// In your locale resolver
'locale_resolver' => fn () => $_GET['lang'] ?? $_SESSION['locale'] ?? 'en'

// Usage: /?lang=fr will serve French compiled templates

'translator' => [
    // ... other config ...
    'translation_driver' => 'groq', // 'groq', 'google', or 'cerebras'
    'drivers' => [
        'groq' => [
            'key' => 'your-groq-api-key',
            'model' => 'llama-3.1-8b-instant',
        ],
        'google' => [
            'key' => 'your-google-translate-key',
        ],
        'cerebras' => [
            'key' => 'your-cerebras-api-key',
            'model' => 'llama-3.3-70b',
        ],
    ],
],