poly-translator
A multi-language translation aggregator library written in Rust, supporting multiple translation service APIs with a modern asynchronous architecture design. This library provides efficient and reliable translation solutions for the Rust ecosystem by aggregating Baidu, Youdao, Caiyun, Alibaba, MyMemory, and other translation services.
β¨ Features
- Multi-Service Aggregation: Aggregates Baidu, Youdao, Caiyun, Alibaba, and MyMemory translation services
- Async-First Design: Built on
async/awaitpattern, fully utilizing Rust's async runtime - Factory Pattern: Unified translator creation through
TranslatorFactoryfor cleaner code - Type Safety: Complete type definitions and error handling for compile-time safety
- Easy Extension: Follows the Open-Closed Principle; new translation services only need to implement the
AsyncTranslatortrait - Comprehensive Testing: Includes unit tests and integration tests to ensure code quality
ποΈ Architecture
poly-translator/
βββ async_translator.rs # Core trait and data structure definitions
βββ translator_factory.rs # Factory pattern implementation
βββ translator_error.rs # Unified error type definitions
βββ baidu_translator.rs # Baidu translation implementation
βββ youdao_translator.rs # Youdao translation implementation
βββ caiyun_translator.rs # Caiyun translation implementation
βββ alibaba_translator.rs # Alibaba translation implementation
βββ mymemory_translator.rs # MyMemory translation implementation
βββ mod.rs # Module entry point
Core Design
- AsyncTranslator Trait: Defines the common interface for translators; all translators must implement this trait
- TranslationOutput: Standardized translation result structure containing translated text and target language information
- TranslationListOutput: Batch translation result structure supporting multiple text translations
- TranslatorError: Unified error enum covering various error scenarios during translation
- TranslatorFactory: Factory class responsible for creating corresponding translator instances based on type
π¦ Installation
Add the dependency in your Cargo.toml:
[]
= "1.0.3"
or
[]
= { = "path/to/poly-translator" }
Or install from Git:
[]
= { = "https://github.com/magicgis/PolyTranslator.git" }
π Quick Start
Basic Usage
use ;
async
Specified Language Translation
use Language;
// Chinese to English
let result = translator
.translate
.await?;
// English to Chinese
let result = translator
.translate
.await?;
// Auto-detect language
let result = translator
.translate
.await?;
Batch Translation
use AsyncTranslator;
let texts = vec!;
let results = translator.translate_vec.await?;
for in texts.iter.zip
Using Different Translation Services
use TranslatorType;
// Baidu translation
let baidu = TranslatorFactory.create_from_env?;
// Youdao translation (requires YOUDAO_APP_KEY and YOUDAO_APP_SECRET)
let youdao = TranslatorFactory.create_from_env?;
// Caiyun translation (requires CAIYUN_TOKEN)
let caiyun = TranslatorFactory.create_from_env?;
// Alibaba translation
let alibaba = TranslatorFactory.create_from_env?;
// MyMemory translation (free service, no API key required)
let mymemory = TranslatorFactory.create_from_env?;
βοΈ Environment Variable Configuration
Environment variables required by each translation service:
| Service | Environment Variable | Description |
|---|---|---|
| Baidu | BAIDU_APP_ID |
Baidu Open Platform App ID |
| Baidu | BAIDU_KEY |
Baidu Open Platform App Secret |
| Youdao | YOUDAO_APP_KEY |
Youdao Open Platform App Key |
| Youdao | YOUDAO_APP_SECRET |
Youdao Open Platform App Secret |
| Caiyun | CAIYUN_TOKEN |
Caiyun Technology API Token |
| Caiyun | CAIYUN_REQUEST_ID |
Caiyun API Request ID (optional, default: "demo") |
You can also configure these variables by creating a .env file in the project root:
# Baidu Translation
BAIDU_APP_ID=your_app_id
BAIDU_KEY=your_app_secret
# Youdao Translation
YOUDAO_APP_KEY=your_app_key
YOUDAO_APP_SECRET=your_app_secret
# Caiyun Translation
CAIYUN_TOKEN=your_token
CAIYUN_REQUEST_ID=demo
π Supported Languages
This library supports the following language combinations:
- Chinese Simplified
- Chinese Traditional
- English
- Japanese
- Korean
- French
- German
- Russian
- Spanish
- Portuguese
- Italian
- Arabic
- And more...
Please refer to each translator's implementation for the specific supported language list.
π§ͺ Testing
This project includes comprehensive test cases:
# Run all tests
# Run specific translator tests
# View test coverage
Test Types
- Unit Tests: Test core functionality of each translator
- Integration Tests: Test connection with real APIs (requires API keys)
- Doc Tests: Ensure correctness of documentation examples
π Performance Characteristics
- Async Concurrency: Uses
tokioasync runtime to support high-concurrency translation requests - Connection Pool: Built-in HTTP connection pool to reduce connection establishment overhead
- Efficient Serialization: Uses
serdefor efficient JSON serialization/deserialization - Low Memory Footprint: Rust's memory safety features ensure low memory usage
π‘οΈ Error Handling
This library uses thiserror to define complete error types:
π§ Custom Extension
Adding New Translation Services
- Create a new translator file (e.g.,
custom_translator.rs) - Implement the
AsyncTranslatortrait - Register the new module in
mod.rs - Add a new variant to the
TranslatorTypeenum - ε¨
TranslatorFactory::create_from_envδΈζ·»ε εε»Ίι»θΎ
Example:
use async_trait;
use crate;
π License
This project is licensed under the MIT License.
MIT License
Copyright (c) 2024 Your Name
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
π€ Contributing
Issues and Pull Requests are welcome!
- Fork this project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Create a Pull Request
π§ Contact
If you have any questions or suggestions, please contact us through:
- Create an Issue
- Send an email to: 63542424@163.com
π Acknowledgments
- Tokio - Async runtime
- Reqwest - HTTP client
- Serde - Serialization framework
- Thiserror - Error handling
If you find this project helpful, please give us a βοΈ!