PHP code example of pollora / pollingo

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

    

pollora / pollingo example snippets


use Pollora\Pollingo\Pollingo;

// Simple translation
$translation = Pollingo::make('your-openai-api-key')
    ->from('en')
    ->to('fr')
    ->text('Welcome to our application')
    ->translate();

// Translation with context
$translation = Pollingo::make('your-openai-api-key')
    ->from('en')
    ->to('fr')
    ->text('Welcome to our platform!')
    ->context('Used in the subject of a welcome email.')
    ->translate();

use Pollora\Pollingo\Pollingo;

$translations = Pollingo::make('your-openai-api-key')
    ->from('en')
    ->to('fr')
    ->group('messages', [
        'welcome' => 'Welcome to our application',
        'goodbye' => 'Goodbye!',
    ])
    ->translate();

use Pollora\Pollingo\Facades\Pollingo;

$translations = Pollingo::from('en')
    ->to('fr')
    ->group('messages', [
        'welcome' => 'Welcome to our application',
        'goodbye' => 'Goodbye!',
    ])
    ->translate();

use Pollora\Pollingo\Pollingo;

class TranslationController
{
    public function __construct(
        private readonly Pollingo $pollingo
    ) {}

    public function translate()
    {
        return $this->pollingo
            ->from('en')
            ->to('fr')
            ->group('messages', [
                'welcome' => 'Welcome to our application',
            ])
            ->translate();
    }
}

$pollingo
    ->from('en')    // Source language (English)
    ->to('fr')      // Target language (French)
    ->translate();

$pollingo->group('emails', [
    'welcome' => [
        'text' => 'Welcome to our platform!',
        'context' => 'Used in the subject of a welcome email.'
    ],
    'greeting' => [
        'text' => 'Hi {name}',
        'context' => 'Personal greeting with user name placeholder'
    ]
]);

use Pollora\Pollingo\Contracts\Translator;

class MyCustomTranslator implements Translator
{
    public function translate(
        array $groups,
        string $targetLanguage,
        ?string $sourceLanguage = null,
        ?string $globalContext = null
    ): array {
        // Your custom translation logic here
    }
}

$pollingo = Pollingo::make()
    ->withTranslator(new MyCustomTranslator())
    ->from('en')
    ->to('fr')
    ->translate();

$pollingo = Pollingo::make(translator: new MyCustomTranslator());

use Pollora\Pollingo\Services\BaseAITranslator;
use Pollora\Pollingo\Contracts\AIClient;

class GoogleAITranslator extends BaseAITranslator implements AIClient
{
    private readonly GoogleClient $client;
    
    public function __construct(
        string $apiKey,
        string $model = 'gemini-pro',
        int $timeout = 120
    ) {
        parent::__construct($model, $timeout);
        $this->client = new GoogleClient($apiKey);
    }
    
    public function translate(array $groups, string $targetLanguage, ?string $sourceLanguage = null, ?string $globalContext = null): array
    {
        // Implementation for Google AI translation
    }
    
    public function chatCompletion(string $model, array $messages, float $temperature = 0.1): string
    {
        // Implementation for Google AI chat completion
    }
    
    protected function getSystemPrompt(): string
    {
        // Optionally override the system prompt for Google AI
        return parent::getSystemPrompt();
    }
}

$pollingo
    ->withGlobalContext('This is for a professional business application')
    ->group('auth', [
        'login' => 'Log in',
        'register' => 'Create an account',
    ])
    ->translate();

$translations = $pollingo
    ->from('en')
    ->to('fr')
    ->group('ui', [
        'save' => 'Save',
        'cancel' => 'Cancel',
    ])
    ->group('messages', [
        'success' => 'Operation completed successfully',
        'error' => 'An error occurred',
    ])
    ->translate();

// config/pollingo.php
return [
    'openai' => [
        'api_key' => env('OPENAI_API_KEY'),
        'model' => env('OPENAI_MODEL', 'gpt-4'),
    ],
];

use Pollora\Pollingo\Contracts\Translator;

class TranslationService
{
    public function __construct(
        private readonly Translator $translator
    ) {}
}

// Select different AI models for different needs
$translations = Pollingo::make('your-openai-api-key')
    ->model('gpt-4.1-nano')  // Smaller, faster model for simple translations
    ->from('en')
    ->to('fr')
    ->text('Hello world')
    ->translate();

// Set custom timeout for long translations
$translations = Pollingo::make('your-openai-api-key')
    ->timeout(180)  // 3 minutes timeout (default: 120 seconds)
    ->from('en')
    ->to('es')
    ->group('legal', $legalTexts)  // Large legal documents
    ->translate();

// Configure retry behavior
$translations = Pollingo::make('your-openai-api-key')
    ->maxRetries(5)           // Number of retries on failure (default: 3)
    ->retryDelay(2000)        // Milliseconds between retries (default: 1000)
    ->from('en')
    ->to('ja')
    ->group('documents', $documents)
    ->translate();

// Chain configuration methods
$translations = Pollingo::make('your-openai-api-key')
    ->model('gpt-4.1-turbo')
    ->timeout(240)
    ->maxRetries(4)
    ->retryDelay(1500)
    ->from('en')
    ->to('zh')
    ->group('marketing', $marketingContent)
    ->translate();
bash
php artisan vendor:publish --tag=pollingo-config