translateplus/translateplus-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

translateplus/translateplus-php

Composer 安装命令:

composer require translateplus/translateplus-php

包简介

Official PHP client library for Translation API - Professional translation service for text, HTML, emails, subtitles, and i18n files

README 文档

README

Official PHP client library for the TranslatePlus API - Professional translation service for text, HTML, emails, subtitles, and i18n files.

Packagist Version PHP Version License

Features

  • Full API Support - All endpoints including text, batch, HTML, email, subtitles, and i18n translation
  • Error Handling - Comprehensive exception handling with detailed error messages
  • Retry Logic - Automatic retry with exponential backoff for failed requests
  • Concurrency Control - Built-in support for parallel translations with configurable concurrency limits
  • Type Safety - Full PHPDoc annotations for better IDE support
  • Production Ready - Connection pooling, rate limiting, and robust error handling

Installation

Install via Composer:

composer require translateplus/translateplus-php

Quick Start

<?php

require 'vendor/autoload.php';

use TranslatePlus\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 !'

API Reference

Client Options

$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)
]);

Translation Methods

Translate Text

$result = $client->translate([
    'text' => 'Hello, world!',
    'source' => 'en',  // Optional, defaults to 'auto'
    'target' => 'fr'   // Required
]);

echo $result['translations']['translation'];

Batch Translation

$result = $client->translateBatch([
    'texts' => ['Hello', 'Goodbye', 'Thank you'],
    'source' => 'en',
    'target' => 'fr'
]);

foreach ($result['translations'] as $translation) {
    echo $translation['translation'] . "\n";
}

Translate HTML

$result = $client->translateHTML([
    'html' => '<p>Hello <b>world</b></p>',
    'source' => 'en',
    'target' => 'fr'
]);

echo $result['html']; // '<p>Bonjour <b>monde</b></p>'

Translate Email

$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>'

Translate Subtitles

$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'];

Language Methods

Detect Language

$result = $client->detectLanguage('Bonjour le monde');
echo $result['language_detection']['language']; // 'fr'
echo $result['language_detection']['confidence']; // 0.95

Get Supported Languages

$result = $client->getSupportedLanguages();
print_r($result['supported_languages']);
// Array (
//     [en] => English
//     [fr] => French
//     [es] => Spanish
//     ...
// )

Account Methods

Get Account Summary

$summary = $client->getAccountSummary();
echo "Credits remaining: " . $summary['credits_remaining'];
echo "Plan: " . $summary['plan_name'];
echo "Concurrency limit: " . $summary['concurrency_limit'];

i18n Translation Jobs

Create i18n Job

$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'];

Get Job Status

$status = $client->getI18nJobStatus('job-123');
echo "Status: " . $status['status']; // 'pending', 'processing', 'completed', 'failed'
echo "Progress: " . $status['progress'] . "%";

List Jobs

$jobs = $client->listI18nJobs([
    'page' => 1,
    'page_size' => 20
]);

foreach ($jobs['results'] as $job) {
    echo "Job {$job['id']}: {$job['status']}\n";
}

Error Handling

The client throws specific exceptions for different error types:

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();
}

Advanced Usage

Concurrent Translations

The client automatically handles concurrency limits. You can configure the maximum concurrent requests:

$client = new Client([
    'api_key' => 'your-api-key',
    'max_concurrent' => 10  // Allow up to 10 concurrent requests
]);

Custom Timeout and Retries

$client = new Client([
    'api_key' => 'your-api-key',
    'timeout' => 60,        // 60 second timeout
    'max_retries' => 5      // Retry up to 5 times
]);

Requirements

  • PHP 7.4 or higher
  • ext-json extension
  • ext-curl extension
  • Composer

License

MIT License - see LICENSE file for details.

Support

Related Libraries

TranslatePlus provides official client libraries for multiple programming languages:

All libraries provide the same comprehensive API support and features.

translateplus/translateplus-php 适用场景与选型建议

translateplus/translateplus-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「i18n」 「translation」 「api」 「localization」 「translate」 「multilingual」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 translateplus/translateplus-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 translateplus/translateplus-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 translateplus/translateplus-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-12