PHP code example of neverendless / translator-client

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

    

neverendless / translator-client example snippets


use Neverendless\Translator\TranslatorClient;
use Neverendless\Translator\CachePolicy;

$translator = new TranslatorClient(
    baseUrl: 'https://translate.yourdomain.com',
    token:   'tok_web_your_service_token',
);

$result = $translator->translate('bonjour tout le monde', 'en');

echo $result->translation;  // "hello everyone"
echo $result->sourceLang;   // "fr"  (normalized ISO code)
echo $result->engine;       // "cache" or "ai"
echo $result->resultCode;   // 0 = SUCCESS

use Neverendless\Translator\CachePolicy;
use Neverendless\Translator\TranslatorException;

try {
    $result = $translator->translate(
        text:        'hola amigo',
        targetLang:  'en',
        requestId:   'optional-correlation-id',  // echoed back in the result
        cachePolicy: CachePolicy::NORMAL,         // default; omit for normal use
    );

    if ($result->isSuccess()) {
        echo $result->translation;   // translated string
        echo $result->sourceLang;    // e.g. "es" — auto-detected, normalized code
        echo $result->targetLang;    // e.g. "en" — normalized code
        echo $result->engine;        // "cache" or "ai"
    } elseif ($result->isFallback()) {
        // Service degraded — translation contains the original text.
        // Safe to display as-is; do not persist as a translated variant.
        echo $result->translation;
    }
} catch (TranslatorException $e) {
    // HTTP 401: bad token · 413: text too large · 422: unsupported language
    // 503: queue full (retry shortly) · 0: network error
    // For structured service errors (413/422/503) getResultCode() gives the stable code.
    error_log(sprintf(
        "Translator error HTTP %d result_code %d: %s",
        $e->getHttpCode(), $e->getResultCode(), $e->getMessage()
    ));
    echo $originalText;  // safe fallback
}

use Neverendless\Translator\CachePolicy;

// Public/global/guild chat — shared cache benefits all players
$result = $translator->translate($chatMessage, 'en', cachePolicy: CachePolicy::NORMAL);

// Private whispers — read cache but do not add to it
$result = $translator->translate($whisperText, 'en', cachePolicy: CachePolicy::NO_STORE);

// GMT sensitive ticket content — skip Redis entirely
$result = $translator->translate($ticketBody, 'en', cachePolicy: CachePolicy::BYPASS);

$translator->translate($text, 'en');       // normalized code (preferred)
$translator->translate($text, 'English');  // full name — still accepted

$health = $translator->health();

if (!$health->isHealthy()) {
    error_log("Translator degraded: redis={$health->redis} ollama={$health->ollama}");
}

$translator = new TranslatorClient(
    baseUrl: 'https://translate.yourdomain.com',
    token:   $token,
    timeout: 150,
);