This site is not affiliated with or endorsed by Cloudflare, Inc. It showcases deployable experiments across most Cloudflare products.
AI & Machine Learning

Text Translator

Translate text with Workers AI at the edge - multilingual translation API without a third-party translation service

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 invalid text (INVALID_TEXT)
  • 400 - Missing or invalid target (INVALID_TARGET)
  • 400 - Invalid source language 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

Use in your project

Copy these files into an existing Worker. Prefer Deployment to try the full experiment first. Source: apps/experiments/text-translator.

DependenciesNoneBindingsWorkers AI (AI binding)PlatformWorkers AI
import { DEFAULT_SOURCE_LANG, MAX_TEXT_LENGTH } from "../constants/defaults";export function validateText(input: string | undefined): string | null {  if (!input || typeof input !== "string") return null;  const trimmed = input.trim();  if (!trimmed) return null;  if (trimmed.length > MAX_TEXT_LENGTH) return null;  return trimmed;}export function validateLang(input: string | undefined): string | null {  if (!input || typeof input !== "string") return null;  const trimmed = input.trim().toLowerCase();  if (!trimmed || trimmed.length > 20) return null;  return trimmed;}export function resolveSourceLang(input: string | undefined): string | null {  if (input === undefined) return DEFAULT_SOURCE_LANG;  return validateLang(input);}

Deployment

Click the deploy button

Deploy to Cloudflare Workers

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 dev

Test 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.2b machine translation model

On this page