# Text Translator (/docs/experiments/text-translator)



Translate text between languages using [Workers AI](https://developers.cloudflare.com/workers-ai/) and Meta's m2m100 model. Send text via query parameters and receive a translation without external API keys.

## Features [#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 [#api-reference]

### GET /translate [#get-translate]

Translates the provided text from a source language to a target language.

<TypeTable
  type="{
  text: {
    description: &#x22;Text to translate (max 10,000 characters)&#x22;,
    type: &#x22;string&#x22;,
    required: true,
  },
  target: {
    description: &#x22;Target language code (e.g. `es`, `fr`, `de`)&#x22;,
    type: &#x22;string&#x22;,
    required: true,
  },
  source: {
    description: &#x22;Source language code (default: `en`)&#x22;,
    type: &#x22;string&#x22;,
  },
}"
/>

#### Example Request [#example-request]

```bash
curl "https://your-worker.workers.dev/translate?text=hello&target=es&source=en"
```

#### Success Response [#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

```json
{
  "text": "hello",
  "source": "en",
  "target": "es",
  "translation": "hola"
}
```

#### Error Response [#error-response]

```json
{
  "error": "Missing or invalid query parameter: text",
  "code": "INVALID_TEXT"
}
```

#### Error Codes [#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 [#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 [#limitations]

* Workers AI is subject to [usage limits](https://developers.cloudflare.com/workers-ai/platform/limits/) by plan
* Input text length is capped
* Single-string translation; no document or batch mode

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/text-translator)
  </Step>

  <Step>
    ### Deploy [#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.
  </Step>

  <Step>
    ### Test your deployment [#test-your-deployment]

    ```bash
    curl "https://your-worker.workers.dev/translate?text=hello&target=es&source=en"
    ```
  </Step>
</Steps>

## Local Development [#local-development]

```bash
cd apps/experiments/text-translator
npm install
npm run dev
```

Test locally:

```bash
curl "http://localhost:8787/translate?text=hello&target=es&source=en"
```

## Cloudflare Features Used [#cloudflare-features-used]

* **[Workers](https://developers.cloudflare.com/workers/)** - Edge compute runtime
* **[Workers AI](https://developers.cloudflare.com/workers-ai/)** - `@cf/meta/m2m100-1.2b` machine translation model
