PHP code example of volcy / translator-core

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


use Volcy\Translator\Drivers\BladeDriver;
use Volcy\Translator\IdStrategies\HashIdStrategy;
use Volcy\Translator\ScanRunner;
use Volcy\Translator\Filesystem\NativeFilesystem;
use Volcy\Translator\ViewIndexPathResolver;

// Create components
$filesystem = new NativeFilesystem();
$resolver = new ViewIndexPathResolver();
$driver = new BladeDriver(new HashIdStrategy());
$scanRunner = new ScanRunner($driver, $filesystem, $resolver, new HashIdStrategy());

// Scan Blade views
$result = $scanRunner->run(
    '/path/to/views',
    '/path/to/indexes',
    'en'
);

echo "Scanned {$result['written']} files\n";
foreach ($result['warnings'] as $warning) {
    echo "Warning: $warning\n";
}

use Volcy\Translator\BuildRunner;
use Volcy\Translator\TranslationDriverResolver;

$config = [
    'translation_driver' => 'groq',
    'drivers' => [
        'groq' => [
            'key' => 'your-groq-api-key',
            'model' => 'llama-3.1-8b-instant',
        ],
    ],
];

$buildRunner = new BuildRunner(
    $filesystem,
    $resolver,
    new TranslationDriverResolver($config)
);

$result = $buildRunner->run(
    '/path/to/indexes',
    'fr',
    'en'
);

echo "Translated {$result['translated']} strings, reused {$result['reused']}\n";

use Volcy\Translator\TranslationCatalog;

$catalog = new TranslationCatalog(
    $filesystem,
    $resolver,
    '/path/to/indexes'
);

// Get translation dictionary for views
$dictionary = $catalog->forViewsAndLocale(
    ['home', 'layout', 'partials.header'],
    'fr'
);

// Apply to rendered HTML
$html = '<h1>Welcome</h1><p>This is the home page</p>';
$translated = $catalog->applyToHtml($html, $dictionary);

use Volcy\Translator\IdStrategies\HashIdStrategy;

$driver = new BladeDriver(new HashIdStrategy());

use Volcy\Translator\IdStrategies\TagPathIdStrategy;

$driver = new BladeDriver(new TagPathIdStrategy());

use Volcy\Translator\IdStrategies\ExplicitIdStrategy;

$driver = new BladeDriver(new ExplicitIdStrategy());

$config = [
    'translation_driver' => 'google',
    'drivers' => [
        'google' => [
            'key' => 'your-google-translate-key',
        ],
    ],
];

$config = [
    'translation_driver' => 'groq',
    'drivers' => [
        'groq' => [
            'key' => 'your-groq-api-key',
            'model' => 'llama-3.1-8b-instant',
        ],
    ],
];

$config = [
    'translation_driver' => 'cerebras',
    'drivers' => [
        'cerebras' => [
            'key' => 'your-cerebras-api-key',
            'model' => 'llama-3.3-70b',
        ],
    ],
];

return [
    'title' => 'Welcome to our site',
    'description' => 'This is the home page',
    'cta_button' => 'Get Started',
];