A high-performance, scalable translation microservice built with Flask and ArgosTranslate, designed for ease of use and integration into any multilingual application.
The microservice provides a robust translation API built on Flask and ArgosTranslate, with features like rate limiting, request throttling, and flexible configuration. The implementation follows best practices with proper error handling, middleware design, and comprehensive testing.
The service can handle various input and output formats, making it easy to integrate with different client applications. It's Docker-ready for easy deployment and includes health checks for monitoring.
- Multiple Translation Pairs: Support for various language pairs (en-es, es-en, en-fr, fr-en, en-de, de-en)
- Rate Limiting: Protect your service with configurable rate limiting
- Request Throttling: Control concurrent request load
- Multiple Input/Output Formats: JSON and plain text support
- Docker Ready: Easy deployment with Docker and Docker Compose
- Configurable: YAML config files and environment variable overrides
- Metrics: Request tracking with detailed headers
- Health Checks: Built-in monitoring endpoint
- Comprehensive API: Well-documented RESTful endpoints
GET /health
Returns service status and installed language packages.
POST /translate
Translates text from source language to target language.
Input Formats:
- JSON body:
{
"text": "Hello world",
"source": "en",
"target": "es",
"outputFormat": "json"
}- Form data with parameters:
text,source,target,outputFormat
Output Formats:
- JSON:
{
"translated": "Hola mundo",
"source": "en",
"target": "es",
"original": "Hello world"
}- Plain text:
Hola mundo
GET /config
Returns current service configuration.
PUT /config
Updates service configuration.
- Docker and Docker Compose
- Python 3.9+ (for development without Docker)
- Clone the repository:
git clone https://github.com/fenix-hub/u18n.git
cd u18n- Start the service using Docker Compose:
docker-compose up -d- The service will be available at http://localhost:5000
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Run the service:
FLASK_APP=app/main.py python -m flask runConfiguration can be provided through:
- Default values (built into the application)
- YAML configuration file (
config/default_config.yml) - Environment variables (highest priority)
| Variable | Description |
|---|---|
CONFIG_PATH |
Path to YAML config file |
RATE_LIMIT_ENABLED |
Enable/disable rate limiting (true/false) |
RATE_LIMIT_RPM |
Requests per minute limit |
RATE_LIMIT_BURST |
Maximum burst size |
THROTTLING_ENABLED |
Enable/disable request throttling (true/false) |
THROTTLING_CONCURRENT |
Maximum concurrent requests |
MAX_CHARS_PER_REQUEST |
Character limit per translation request |
AVAILABLE_PACKAGES |
Comma-separated list of language pairs (e.g., "en-es,es-en") |
LOGGING_LEVEL |
Logging level (e.g., DEBUG, INFO, WARNING, ERROR) |
LOGGING_FORMAT |
Python logging format string |
logging:
level: "INFO"
format: "%(asctime)s - PID:%(process)d - %(name)s - %(levelname)s - %(message)s"
rate_limit:
requests_per_minute: 60
burst: 10
enabled: true
throttling:
concurrent_requests: 5
enabled: true
translation:
max_chars_per_request: 5000
available_packages:
- "en-es"
- "es-en"
- "en-fr"
- "fr-en"
- "en-de"
- "de-en"
- "it-en"
- "en-it"
fallback_response: "Translation service unavailable. Please try again later."
formats:
input:
- "json"
- "text"
output:
- "json"
- "text"The service adds informative headers to responses:
X-RateLimit-Limit: Maximum requests per minuteX-RateLimit-Remaining: Remaining tokensX-RateLimit-Reset: Seconds until full resetRetry-After: Seconds to wait when rate limited
X-Throttle-Limit: Maximum concurrent requestsX-Throttle-Usage: Current usageX-Throttle-Remaining: Available slots
X-Translation-Characters: Character count of original text
Run tests with pytest:
pytestRun with coverage report:
pytest --cov=app tests/The project follows a modular architecture with separate components for:
- API Layer: RESTful endpoints and request handling
- Service Layer: Business logic implementation
- Configuration Management: Dynamic config loading
- Middleware: Rate limiting and request throttling
- Utilities: Helper functions and decorators
translation-service/
βββ Dockerfile # Docker image definition
βββ docker-compose.yml # Docker composition for easy deployment
βββ requirements.txt # Python dependencies
βββ README.md # Project documentation
βββ config/ # Configuration management
β βββ __init__.py
β βββ default_config.yml # Default configuration values
β βββ config_loader.py # Configuration loading logic
βββ app/ # Application code
β βββ __init__.py # Logging configuration
β βββ main.py # Application entry point
β βββ api/ # API endpoints
β β βββ __init__.py
β β βββ routes.py # Route definitions
β β βββ middleware.py # Request middleware
β βββ services/ # Business logic services
β β βββ __init__.py
β β βββ translation_service.py # Translation logic
β β βββ rate_limiter.py # Rate limiting service
β β βββ request_throttler.py # Request throttling service
β βββ utils/ # Utility functions
β βββ __init__.py
β βββ helpers.py # Helper decorators and functions
βββ tests/ # Test suite
βββ __init__.py
βββ test_api.py # API tests
βββ test_translation.py # Translation service tests
βββ test_config.py # Configuration tests
This service uses ArgosTranslate, an open-source neural machine translation library that allows for offline translation. It supports multiple language pairs and uses lightweight models suitable for containerized deployments.
- Model Download: Language models are downloaded on first use, which may cause initial delays
- Memory Usage: Consider container memory limits based on the number of language pairs loaded
- Caching: API responses are not cached; consider implementing a caching layer for frequent translations
- Scaling: For high-volume scenarios, deploy multiple instances behind a load balancer
- The service does not implement authentication; consider adding API keys or OAuth2
- Request rates are tracked by IP address; implement user-based tracking for multi-user scenarios
- Add HTTPS for production deployments
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.