A Go-based microservice that provides text translation via Google Translate API with Redis caching.
- HTTP API for translation requests
- Automatic language detection (when source language is not specified)
- Redis caching with 2-week TTL
- Docker and Docker Compose support for easy deployment
- Health check endpoint
- Go 1.21 or higher
- Redis server
- Google Cloud Platform account with Translation API enabled
- Google Cloud service account credentials
git clone https://github.com/yourusername/translation-service.git
cd translation-service
- Create a Google Cloud project
- Enable the Cloud Translation API
- Create a service account with Translation API access
- Download the service account key as JSON
- Save the JSON file as
credentials.json
in the project root or set theGOOGLE_APPLICATION_CREDENTIALS
environment variable to point to your credentials file
Create a .env
file based on the example:
cp .env.example .env
Edit the .env
file to match your configuration.
This is the easiest way to get up and running:
docker-compose up -d
If you prefer to run the service locally:
- Start a Redis server
- Install dependencies:
go mod download
- Run the service:
go run main.go
Endpoint: POST /translate
Request Body:
{
"text": "Hello, world!",
"source_lang": "en", // Optional: ISO 639-1 language code
"target_lang": "es" // Required: ISO 639-1 language code
}
If source_lang
is omitted, the service will auto-detect the source language.
Response:
{
"translated_text": "¡Hola, mundo!",
"source_lang": "en",
"target_lang": "es",
"cache_hit": false
}
curl -X POST \
http://localhost:8080/translate \
-H "Content-Type: application/json" \
-d '{
"text": "Hello, world!",
"source_lang": "en",
"target_lang": "es"
}' \
-o response.json
Endpoint: GET /health
Returns 200 OK
if the service and Redis are functioning properly.
The service caches translation results in Redis with a 2-week TTL (time to live). The cache key is constructed using the source language, target language, and input text.
- For production deployments, consider adding authentication to the API
- Set up proper Redis security (password, firewall, etc.)
- Implement rate limiting to prevent excessive API usage
- Use environment variables for all configuration in production
MIT