PHPackages                             moyefu/translator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. moyefu/translator

ActiveLibrary[Localization &amp; i18n](/categories/localization)

moyefu/translator
=================

A PHP library for integrating with various translation APIs (Baidu, Google, Youdao, Tencent, Ali, Volcengine, Microsoft, etc.)

1.0.0(2mo ago)15MITPHPPHP &gt;=5.6

Since Apr 10Pushed 2mo agoCompare

[ Source](https://github.com/moyefu/translator)[ Packagist](https://packagist.org/packages/moyefu/translator)[ RSS](/packages/moyefu-translator/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Translator Library
==================

[](#translator-library)

[中文文档](README-zh.md) | [English Documentation](README.md)

A PHP library for integrating with various translation APIs (Baidu, Google, Youdao, etc.). This library provides a unified interface to easily switch between different translation services without changing your code.

Features
--------

[](#features)

- Supports multiple translation APIs (Baidu, Google, Youdao, Tencent, Ali, Volcengine, Microsoft)
- Unified interface for all translators
- Easy to extend with new translation services
- Comprehensive error handling
- Well-documented API
- Unit tests included

Requirements
------------

[](#requirements)

- PHP &gt;= 5.6
- GuzzleHttp &gt;= 6.3

Installation
------------

[](#installation)

You can install the library via Composer:

```
composer require moyefu/translator
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
require __DIR__ . '/vendor/autoload.php';

use Moyefu\TranslatorFactory;

// Create Baidu translator
$baiduTranslator = TranslatorFactory::create('baidu', [
    'appId' => 'your_baidu_app_id',
    'key' => 'your_baidu_key'
]);

// Translate text
try {
    $result = $baiduTranslator->translate('Hello world', 'en', 'zh');
    echo 'Translation result: ' . $result;
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

// Create Google translator
$googleTranslator = TranslatorFactory::create('google', [
    'key' => 'your_google_key'
]);

// Create Youdao translator
$youdaoTranslator = TranslatorFactory::create('youdao', [
    'appId' => 'your_youdao_app_id',
    'key' => 'your_youdao_key'
]);

// Create Tencent translator
$tencentTranslator = TranslatorFactory::create('tencent', [
    'secretId' => 'your_tencent_secret_id',
    'secretKey' => 'your_tencent_secret_key'
]);

// Create Ali translator
$aliTranslator = TranslatorFactory::create('ali', [
    'accessKeyId' => 'your_ali_access_key_id',
    'accessKeySecret' => 'your_ali_access_key_secret'
]);

// Create Volcengine translator
$volcengineTranslator = TranslatorFactory::create('volcengine', [
    'accessKeyId' => 'your_volcengine_access_key_id',
    'accessKeySecret' => 'your_volcengine_access_key_secret'
]);

// Create Microsoft translator
$microsoftTranslator = TranslatorFactory::create('microsoft', [
    'apiKey' => 'your_microsoft_api_key',
    'endpoint' => 'your_microsoft_endpoint'
]);
```

### Advanced Usage

[](#advanced-usage)

```
// Set additional options
$translator->setOptions([
    'timeout' => 10, // 10 seconds timeout
    'verify' => true // Verify SSL certificates
]);

// Change API key dynamically
$translator->setKey('new_api_key');

// Translate multiple sentences
$texts = [
    'Hello world',
    'How are you?',
    'I am fine, thank you.'
];

foreach ($texts as $text) {
    try {
        $result = $translator->translate($text, 'en', 'zh');
        echo "Original: $text\n";
        echo "Translation: $result\n\n";
    } catch (Exception $e) {
        echo "Error translating '$text': " . $e->getMessage() . "\n";
    }
}
```

### Supported Translators

[](#supported-translators)

TranslatorAPI ReferenceRequired Configuration**baidu**[Baidu Translate API](docs/platforms/baidu.md)`['appId' => '...', 'key' => '...']`**google**[Google Translate API](docs/platforms/google.md)`['key' => '...']`**youdao**[Youdao Translate API](docs/platforms/youdao.md)`['appId' => '...', 'key' => '...']`**tencent**[Tencent Translate API](docs/platforms/tencent.md)`['secretId' => '...', 'secretKey' => '...']`**ali**[Ali Translate API](docs/platforms/ali.md)`['accessKeyId' => '...', 'accessKeySecret' => '...']`**volcengine**[Volcengine Translate API](docs/platforms/volcengine.md)`['accessKeyId' => '...', 'accessKeySecret' => '...']`**microsoft**[Microsoft Translate API](docs/platforms/microsoft.md)`['apiKey' => '...', 'endpoint' => '...']`API Reference
-------------

[](#api-reference)

### TranslatorFactory::create($type, array $config = \[\])

[](#translatorfactorycreatetype-array-config--)

Create a translator instance.

- `$type`: Translator type (baidu, google, youdao, tencent, ali, volcengine, microsoft)
- `$config`: Configuration array
    - For Baidu: `['appId' => '...', 'key' => '...']`
    - For Google: `['key' => '...']`
    - For Youdao: `['appId' => '...', 'key' => '...']`
    - For Tencent: `['secretId' => '...', 'secretKey' => '...']`
    - For Ali: `['accessKeyId' => '...', 'accessKeySecret' => '...']`
    - For Volcengine: `['accessKeyId' => '...', 'accessKeySecret' => '...']`
    - For Microsoft: `['apiKey' => '...', 'endpoint' => '...']`

### TranslatorInterface

[](#translatorinterface)

All translators implement the `TranslatorInterface` with the following methods:

- `translate($text, $from, $to)`: Translate text from source language to target language

    - `$text`: Text to translate
    - `$from`: Source language code (e.g., 'en', 'zh')
    - `$to`: Target language code (e.g., 'zh', 'en')
    - Returns: Translated text
    - Throws: Exception if translation fails
- `setKey($key)`: Set API key

    - `$key`: API key for the translation service
- `setOptions(array $options)`: Set additional options

    - `$options`: Array of options (e.g., timeout, verify)

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/phpunit tests/TranslatorTest.php
```

Example
-------

[](#example)

Check out the [example.php](example.php) file for a complete example of how to use the library.

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

License
-------

[](#license)

MIT

###  Health Score

33

—

LowBetter than 73% of packages

Maintenance88

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

60d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1104e7bf183c5cad497171690937eb57d0f24ce905cd74cca302da03e8cc886c?d=identicon)[moyefu](/maintainers/moyefu)

---

Top Contributors

[![moyefu](https://avatars.githubusercontent.com/u/57945333?v=4)](https://github.com/moyefu "moyefu (30 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/moyefu-translator/health.svg)

```
[![Health](https://phpackages.com/badges/moyefu-translator/health.svg)](https://phpackages.com/packages/moyefu-translator)
```

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k532.1M2.5k](/packages/aws-aws-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

1.9k496.1k32](/packages/neuron-core-neuron-ai)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3751.2M45](/packages/tencentcloud-tencentcloud-sdk-php)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k11](/packages/tempest-framework)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

232.5k](/packages/eslazarev-wildberries-sdk)[files.com/files-php-sdk

Files.com PHP SDK

2478.1k](/packages/filescom-files-php-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
