Download the PHP package moyefu/translator without Composer
On this page you can find all versions of the php package moyefu/translator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download moyefu/translator
More information about moyefu/translator
Files in moyefu/translator
Package translator
Short Description A PHP library for integrating with various translation APIs (Baidu, Google, Youdao, Tencent, Ali, Volcengine, Microsoft, etc.)
License MIT
Informations about the package translator
Translator Library
English Documentation
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
- 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
- PHP >= 5.6
- GuzzleHttp >= 6.3
Installation
You can install the library via Composer:
Usage
Basic Usage
Advanced Usage
Supported Translators
| Translator | API Reference | Required Configuration |
|---|---|---|
| baidu | Baidu Translate API | ['appId' => '...', 'key' => '...'] |
| Google Translate API | ['key' => '...'] |
|
| youdao | Youdao Translate API | ['appId' => '...', 'key' => '...'] |
| tencent | Tencent Translate API | ['secretId' => '...', 'secretKey' => '...'] |
| ali | Ali Translate API | ['accessKeyId' => '...', 'accessKeySecret' => '...'] |
| volcengine | Volcengine Translate API | ['accessKeyId' => '...', 'accessKeySecret' => '...'] |
| microsoft | Microsoft Translate API | ['apiKey' => '...', 'endpoint' => '...'] |
API Reference
TranslatorFactory::create($type, 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' => '...']
- For Baidu:
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
Run the tests with:
Example
Check out the example.php file for a complete example of how to use the library.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT