承接 opencat/mt 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

opencat/mt

Composer 安装命令:

composer require opencat/mt

包简介

Machine translation adapter framework for the OpenCAT Framework

README 文档

README

Machine translation adapters for the OpenCAT Framework.

Provides DeepLAdapter and GoogleTranslateAdapter, both built on PSR-18 HTTP client injection. Inline codes (InlineCode objects) survive the MT round-trip as numbered XML placeholders.

Installation

composer require opencat/mt

You also need a PSR-18 HTTP client. Guzzle is the most common choice:

composer require guzzlehttp/guzzle

DeepL adapter

use CatFramework\Mt\DeepL\DeepLAdapter;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory;

$http    = new GuzzleClient();
$factory = new HttpFactory();

$adapter = new DeepLAdapter(
    httpClient: $http,
    requestFactory: $factory,
    streamFactory: $factory,
    apiKey: 'your-deepl-api-key',
    // Free tier keys end with ":fx" — the adapter selects the correct endpoint automatically
);

// Translate a single segment
$translated = $adapter->translate($sourceSegment, 'en-US', 'fr-FR');

// Translate many segments in one HTTP request
$translatedBatch = $adapter->translateBatch($segments, 'en-US', 'fr-FR');

DeepL free-tier keys (ending in :fx) are routed to api-free.deepl.com automatically. Pro keys go to api.deepl.com.

On 429 Too Many Requests or 5xx errors the adapter retries up to three times with 1-second delays (configurable via $retryDelays):

$adapter = new DeepLAdapter($http, $factory, $factory, $apiKey, retryDelays: [500_000, 1_000_000]);
// 0.5 s then 1 s before giving up

Google Translate adapter

use CatFramework\Mt\Google\GoogleTranslateAdapter;

$adapter = new GoogleTranslateAdapter(
    httpClient: $http,
    requestFactory: $factory,
    streamFactory: $factory,
    apiKey: 'your-google-api-key',
    projectId: 'your-gcp-project-id',
);

$translated = $adapter->translate($sourceSegment, 'en', 'fr');

Null adapter (testing)

NullMtAdapter implements MachineTranslationInterface and returns copies of the source segment unchanged. Use it in tests or dry-runs where you don't want to call a live API:

use CatFramework\Mt\NullMtAdapter;

$adapter = new NullMtAdapter();
$result  = $adapter->translate($segment, 'en', 'fr');
// $result->getPlainText() === $segment->getPlainText()

Inline code preservation

InlineCode elements are converted to <x id="N"/> placeholders before sending to the MT API (tag_handling=xml for DeepL). The MT engine treats them as opaque tokens and repositions them in the translated text. On the way back they are restored to the original InlineCode objects.

If the MT response is malformed XML, the adapter falls back to stripping all <x …/> tags and returning a plain-text segment — degraded output is better than a crash.

Error codes

MtException carries a typed error code:

Code constant Meaning
MtException::AUTH_FAILED 403 — invalid API key
MtException::RATE_LIMITED 429 — too many requests
MtException::QUOTA_EXCEEDED 456 — DeepL character quota exceeded
MtException::BAD_REQUEST 4xx other
MtException::SERVER_ERROR 5xx

Custom adapter

Extend AbstractMtAdapter and implement the MachineTranslationInterface contract. The base class provides encodeSegment(), decodeXml(), sendRequest(), and retry():

use CatFramework\Mt\AbstractMtAdapter;
use CatFramework\Core\Model\Segment;

class MyMtAdapter extends AbstractMtAdapter
{
    public function translate(Segment $source, string $sourceLanguage, string $targetLanguage): Segment
    {
        ['text' => $text, 'map' => $map] = $this->encodeSegment($source);
        // send $text to your MT API ...
        $responseText = '...';
        return $this->decodeXml($responseText, $map, $source->id . '-mt');
    }

    public function translateBatch(array $sources, string $sourceLanguage, string $targetLanguage): array
    {
        return array_map(fn($s) => $this->translate($s, $sourceLanguage, $targetLanguage), $sources);
    }

    public function getProviderId(): string { return 'my-mt'; }
}

Related packages

  • opencat/coreMachineTranslationInterface, Segment, InlineCode, MtException
  • opencat/workflow — uses MachineTranslationInterface to fill segments below the TM threshold

opencat/mt 适用场景与选型建议

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

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

围绕 opencat/mt 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-09