PHP code example of juksgraphic / bladetranslator

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

    

juksgraphic / bladetranslator example snippets


use Juksgraphic\BladeTranslator\Dto\TranslatorConfig;

$config = new TranslatorConfig(
    sourceLocale: 'fr',
    targetLocales: ['en', 'es'],
    translationsPath: resource_path('lang'),
    viewsPath: resource_path('views'),
    batchSize: 10,
);

use Juksgraphic\BladeTranslator\PageTranslator;
use Juksgraphic\BladeTranslator\Providers\CerebrasProvider; // OpenAI, Cerebras, etc.

$provider   = new CerebrasProvider("your-cerebras-api-key" , "your-model-use");

$translator = PageTranslator::make($provider, $config)

// 1. Extract translatable keys from a Blade view
$extractedKeys = $translator->generate(resource_path('views/pages/about.blade.php'));

// 2. Translate the domain/slug into configured target locales
$translations = $translator->translate('pages/about', ['en', 'es']);

use Juksgraphic\BladeTranslator\TranslationLoader;

$loader = new TranslationLoader(
    store: $store,
    locale: 'en',
    fallbackLocales: ['fr'],
    onError: function (\Throwable $e) {
        logger()->error('Translation loading error: ' . $e->getMessage());
    }
);

// Returns: "Hello John" (interpolated from "Hello :user" or "Hello {user}")
echo $loader->get('pages/about', 'welcome', ['user' => 'John']);