I Only Write in Chinese, English Is Handled by Cloudflare Workers AI
I only write in Chinese, but my site is bilingual. Making it bilingual used to mean uploading two versions of every post to the CMS, with the English done in Google Translate or by a professional translator. It was tedious and costly. Now one translate button gives me an English draft.
This post uses the translation feature on my own site as a case study. It documents how I connected Cloudflare Workers AI, the two times things went wrong, and what I added to the code afterward to make the translation trustworthy.
Why I Chose Workers AI
I originally thought I would need a Gemini API key for my project, then found that Cloudflare has Workers AI built in. My site already runs on Cloudflare Workers and the posts live in R2, so Workers AI was the easiest choice. There is no separate API key to apply for, and no key to hide in environment variables and rotate. I add one line of binding to wrangler.jsonc, and the Worker gets env.AI:
"ai": { "binding": "AI" }On cost, Workers AI has a free tier of 10,000 Neurons per day, which is more than enough for me to occasionally translate one article. The translation API is only callable in edit mode, so readers cannot reach it, and the quota will not be used up by anyone else.
The model I use is @cf/qwen/qwen3.8-27b. You call it like any chat model, with a system prompt plus the content to translate:
const result = await env.AI.run(MODEL, {
messages: [
{ role: 'system', content: SYSTEM },
{ role: 'user', content: input },
],
reasoning_effort: 'low',
max_completion_tokens: 12000,
})Translate Text Only, Don't Touch Layout
My posts are Tiptap documents with paragraphs, blockquotes, images, code blocks, and carousels. If I sent the entire JSON to the model, it might change attributes, translate code, or break the document structure.
So the frontend splits the document first and only sends the strings that contain Chinese: paragraph and heading text, image alt text and captions, video and demo captions, gallery titles and descriptions, and blockquote attributions. Of the code blocks, only those set to plain text get translated, since I often use them for prompts. All other code is skipped. After translation, each piece goes back to its original position.
One block is one string, so if a sentence has bold or a link in the middle, the whole sentence gets translated together instead of being chopped into pieces. Bold and links are carried along using markers, for example <t0>bold text</t0>. The model just needs to keep the markers, and after translation the formatting can be reapplied to the right words.
Long articles go out in batches because the model times out when one request carries too much text. In my tests it often failed past about 2,000 characters, and 1,000 characters take about 45 seconds. So the frontend splits the strings into batches of at most 1,000 characters and translates them one batch at a time.
First Failure: Names and Numbers Translated Wrong
The first time I actually used the About page for translation, the English read smoothly, but there were three factual errors: my name became "Zhu TEKUEI", company names I used to work at were left in Chinese without being translated, and "more than ten exhibitions" became "over a dozen".
The fix was to make the rules more specific: separate personal names from brand names, list the English names directly for former companies and the three case studies (MONOLAB, Matsu Biennial, Lustre Yellow), keep all numbers exactly as in the original, and "10 餘場" means more than ten, not rounded up to a dozen.
- Keep every number, year and quantity exact. 10 餘場 means "more than ten", never "over a dozen".
- 朱德溎 (his full name): Te-Kuei Chu.
- TEKUEI 德溎 or 德溎 alone (the brand): TEKUEI.
- 墨雨互動設計: MONOLAB. 馬祖國際藝術島: Matsu Biennial. 花蜜水晶花工藝: Lustre Yellow.This taught me that smooth reading does not mean accurate translation. The things models are most likely to get wrong are exactly the things readers care most about: personal names, company names, and numbers. And if I do not spell these out, they can only guess.
Second Failure: An Article Broke Halfway Through
I then tried translating a longer Journal article, and the API returned a 502 with no explanation.
At that point I was asking the model to return JSON with an id for each string. I did not reproduce this error on the production model, but I suspect two possibilities. One is that the article had many short fragments, and the model merged several into one sentence, so the count did not match. The other is that quotation marks appeared in the English translation and broke the JSON directly.
Either way, I cannot make the model never make mistakes. I can only make the code check its work. So I changed three things. First, both input and output now use tags, with each string wrapped in <s0>…</s0>, so quotation marks in the translation will not break anything. Second, if some results are missing, only the missing ones are sent again. Third, if items are still missing after a retry, the error message will show how many came back and how many there should be in total. The log only records the count, not the article content.
function readReply(reply, into) {
for (const m of String(reply ?? '').matchAll(/<s(\d+)>([\s\S]*?)<\/s\1>/g)) {
const text = m[2].trim()
if (text) into.set(Number(m[1]), text)
}
}The Last Step Left for Humans
Translation does not auto-save. The button only puts the result into the English draft. I switch to the English version, read through it, fix anything that does not flow, and then save. If the English version already has content, it asks first whether I want to overwrite, so my manually edited English will not be replaced by the next translation.
For me, what this button saves is not translation time, but the procrastination of "I will fill in the English version later." Now I write in Chinese, press the button, and the English version already has a draft I can work from. All that is left is proofreading.
If you also want to add translation to your own Cloudflare project, my advice is to first write the rules for personal names, company names, and numbers into the prompt, then assume the model's response will always be missing something, and make the code able to fill it in on its own.