ecosplay/e-translate
Composer 安装命令:
composer require ecosplay/e-translate
包简介
PHP client for the e-translate API (translation.e-cosplay.fr): text & HTML translation, language detection, key authentication.
README 文档
README
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
| Method | Endpoint | Returns |
|---|---|---|
translate($message, $from, $to, $format='text') | POST /translate | TranslateResult { translatedText, cached, detectedLanguage? } |
detect($text) | POST /detect | DetectedLanguage[] { language, confidence } |
languages() | GET /languages | Language[] { code, name, targets } |
health() | GET /health | HealthResult { 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
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-13