Text Translator
Translate text with Workers AI at the edge
Translate text between languages using Workers AI and Meta's m2m100 model. Send text via query parameters and receive a translation without external API keys.
Features
- Translate text between languages using Workers AI
- Machine translation model (
@cf/meta/m2m100-1.2b) at the edge - Stateless GET API with source text and target language
- No third-party translation service required
API Reference
GET /translate
Translates the provided text from a source language to a target language.
Prop
Type
Example Request
curl "https://your-worker.workers.dev/translate?text=hello&target=es&source=en"Success Response
text string
The original input text
source string
Source language code used for translation
target string
Target language code used for translation
translation string
The translated text
{
"text": "hello",
"source": "en",
"target": "es",
"translation": "hola"
}Error Response
{
"error": "Missing or invalid query parameter: text",
"code": "INVALID_TEXT"
}Error Codes
400- Missing or invalidtext(INVALID_TEXT)400- Missing or invalidtarget(INVALID_TARGET)400- Invalidsourcelanguage code (INVALID_SOURCE)502- Workers AI model run failed (AI_ERROR)
Use Cases
- Add on-the-fly translation to edge APIs and proxies
- Localize user-facing strings without a third-party translation service
- Learn Workers AI text-to-text models in a stateless Worker
- Prototype multilingual chatbots and content tools at the edge
Limitations
- Workers AI is subject to usage limits by plan
- Input text length is capped
- Single-string translation; no document or batch mode
Deployment
Deploy
Enable the Workers AI binding (AI) in your Worker settings. The deploy button configures this automatically via wrangler.json. Requires a Cloudflare account with Workers AI enabled.
Test your deployment
curl "https://your-worker.workers.dev/translate?text=hello&target=es&source=en"Local Development
cd apps/experiments/text-translator
npm install
npm run devTest locally:
curl "http://localhost:8787/translate?text=hello&target=es&source=en"Cloudflare Features Used
- Workers - Edge compute runtime
- Workers AI -
@cf/meta/m2m100-1.2bmachine translation model