PHP code example of translateplus / translateplus-php
1. Go to this page and download the library: Download translateplus/translateplus-php 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/ */
translateplus / translateplus-php example snippets
ranslatePlus\Client;
// Initialize client
$client = new Client([
'api_key' => 'your-api-key'
]);
// Translate a single text
$result = $client->translate([
'text' => 'Hello, world!',
'source' => 'en',
'target' => 'fr'
]);
echo $result['translations']['translation']; // 'Bonjour le monde !'
$client = new Client([
'api_key' => 'your-api-key', // Required: Your TranslatePlus API key
'base_url' => 'https://api.translateplus.io', // Optional: API base URL
'timeout' => 30, // Optional: Request timeout in seconds (default: 30)
'max_retries' => 3, // Optional: Maximum retries (default: 3)
'max_concurrent' => 5, // Optional: Max concurrent requests (default: 5)
]);
$result = $client->translate([
'text' => 'Hello, world!',
'source' => 'en', // Optional, defaults to 'auto'
'target' => 'fr' // Required
]);
echo $result['translations']['translation'];
$result = $client->translateBatch([
'texts' => ['Hello', 'Goodbye', 'Thank you'],
'source' => 'en',
'target' => 'fr'
]);
foreach ($result['translations'] as $translation) {
echo $translation['translation'] . "\n";
}
$result = $client->translateHTML([
'html' => '<p>Hello <b>world</b></p>',
'source' => 'en',
'target' => 'fr'
]);
echo $result['html']; // '<p>Bonjour <b>monde</b></p>'
$result = $client->translateEmail([
'subject' => 'Welcome',
'email_body' => '<p>Thank you for signing up!</p>',
'source' => 'en',
'target' => 'fr'
]);
echo $result['subject']; // 'Bienvenue'
echo $result['html_body']; // '<p>Merci de vous être inscrit!</p>'
$result = $client->translateSubtitles([
'content' => "1\n00:00:01,000 --> 00:00:02,000\nHello world\n",
'format' => 'srt', // or 'vtt'
'source' => 'en',
'target' => 'fr'
]);
echo $result['content'];
$result = $client->detectLanguage('Bonjour le monde');
echo $result['language_detection']['language']; // 'fr'
echo $result['language_detection']['confidence']; // 0.95
$result = $client->getSupportedLanguages();
print_r($result['supported_languages']);
// Array (
// [en] => English
// [fr] => French
// [es] => Spanish
// ...
// )
$summary = $client->getAccountSummary();
echo "Credits remaining: " . $summary['credits_remaining'];
echo "Plan: " . $summary['plan_name'];
echo "Concurrency limit: " . $summary['concurrency_limit'];
$result = $client->createI18nJob([
'file_path' => '/path/to/locales/en.json',
'target_languages' => ['fr', 'es', 'de'],
'source_language' => 'en',
'webhook_url' => 'https://example.com/webhook' // Optional
]);
echo "Job ID: " . $result['job_id'];
$status = $client->getI18nJobStatus('job-123');
echo "Status: " . $status['status']; // 'pending', 'processing', 'completed', 'failed'
echo "Progress: " . $status['progress'] . "%";
$jobs = $client->listI18nJobs([
'page' => 1,
'page_size' => 20
]);
foreach ($jobs['results'] as $job) {
echo "Job {$job['id']}: {$job['status']}\n";
}
use TranslatePlus\TranslatePlusError;
use TranslatePlus\TranslatePlusAPIError;
use TranslatePlus\TranslatePlusAuthenticationError;
use TranslatePlus\TranslatePlusRateLimitError;
use TranslatePlus\TranslatePlusInsufficientCreditsError;
use TranslatePlus\TranslatePlusValidationError;
try {
$result = $client->translate([
'text' => 'Hello',
'target' => 'fr'
]);
} catch (TranslatePlusAuthenticationError $e) {
echo "Authentication failed: " . $e->getMessage();
} catch (TranslatePlusInsufficientCreditsError $e) {
echo "Insufficient credits: " . $e->getMessage();
echo "Status code: " . $e->getStatusCode();
} catch (TranslatePlusRateLimitError $e) {
echo "Rate limit exceeded: " . $e->getMessage();
} catch (TranslatePlusAPIError $e) {
echo "API error: " . $e->getMessage();
echo "Status code: " . $e->getStatusCode();
print_r($e->getResponse());
} catch (TranslatePlusValidationError $e) {
echo "Validation error: " . $e->getMessage();
}
$client = new Client([
'api_key' => 'your-api-key',
'max_concurrent' => 10 // Allow up to 10 concurrent requests
]);
$client = new Client([
'api_key' => 'your-api-key',
'timeout' => 60, // 60 second timeout
'max_retries' => 5 // Retry up to 5 times
]);
bash
composer