ecosplay/e-translate

PHP client for the e-translate API (translation.e-cosplay.fr): text & HTML translation, language detection, key authentication.

Maintainers

Package info

code.e-cosplay.fr/shoko/e-translate-sdk-php

Homepage

pkg:composer/ecosplay/e-translate

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

v1.1.2 2026-06-13 08:48 UTC

This package is auto-updated.

Last update: 2026-06-13 08:52:53 UTC


README

Packagist Version PHP Version License

Quality Gate Status Coverage Duplicated Lines (%) Lines of Code Security Hotspots Reliability Rating Maintainability Rating Security Rating

PHP client for the e-translate API (translation through a key-authenticated API, with built-in caching).

  • โœ… PHP 8.1+, only ext-curl + ext-json (no runtime dependencies)
  • โœ… Text & HTML translation, batch, language auto-detection
  • โœ… Typed exception (ETranslateException) with HTTP status + body

๐Ÿ“ฆ Installation

composer require ecosplay/e-translate

๐Ÿš€ Usage

<?php
use Ecosplay\ETranslate\Client;
use Ecosplay\ETranslate\ETranslateException;

require 'vendor/autoload.php';

$client = new Client('https://translation.e-cosplay.fr', 'etk_xxxxxxxx');

// Simple text โ€” returns a typed TranslateResult object
$res = $client->translate('Bonjour le monde', 'fr', 'en');
echo $res->translatedText;             // "Hello world"
echo $res->cached ? 'cache' : 'fresh';

// HTML (markup preserved)
$client->translate('<b>Bonjour</b>', 'fr', 'en', 'html');

// Several texts at once
$res = $client->translate(['Cat', 'Dog'], 'en', 'fr');
print_r($res->translatedText);         // ['Chat', 'Chien']

// Auto-detect the source language
$r = $client->translate('Guten Tag', 'auto', 'fr');
echo $r->detectedLanguage->language;   // "de"
echo $r->detectedLanguage->confidence; // 90

// Other endpoints (typed objects)
$langs = $client->languages();         // Language[]          -> $langs[0]->code
$det   = $client->detect('Bonjour');   // DetectedLanguage[]  -> $det[0]->language
$h     = $client->health();            // HealthResult        -> $h->status, $h->engine

Options

$client = new Client('https://translation.e-cosplay.fr', 'etk_xxx', [
    'timeoutMs' => 20000,                 // per-request timeout (default 20000)
    'headers'   => ['X-Trace' => 'abc'],  // extra headers
]);

Error handling

try {
    $client->translate('x', 'fr', 'en');
} catch (ETranslateException $e) {
    echo $e->status;        // 401 (invalid key), 429 (rate limit), 502 (upstream), 0 (network/timeout)
    echo $e->getMessage();
    var_dump($e->body);     // decoded JSON body returned by the server, if any
}

๐Ÿ“š API

MethodEndpointReturns
translate($message, $from, $to, $format='text')POST /translateTranslateResult { translatedText, cached, detectedLanguage? }
detect($text)POST /detectDetectedLanguage[] { language, confidence }
languages()GET /languagesLanguage[] { code, name, targets }
health()GET /healthHealthResult { status, redis, engine, time }

All results are readonly objects (Ecosplay\ETranslate\*), so access fields with -> (e.g. $res->detectedLanguage->language). $message accepts a string or an array of strings, $from accepts "auto"; the server aliases source/target/q are also accepted.

๐Ÿงช Tests & coverage

Tests run against an in-process mock server (tests/router.php started via php -S), so no real backend is required.

composer install
composer test        # run the suite
composer coverage    # coverage -> coverage/clover.xml (used by SonarQube)

Quality & coverage are reported to SonarQube by CI.

License

MIT