<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Cloudflare on Deux</title><link>https://deux.top/en/tags/cloudflare/</link><description>Recent content in Cloudflare on Deux</description><image><title>Deux</title><url>https://deux.top/branding/og-default.jpg</url><link>https://deux.top/branding/og-default.jpg</link></image><generator>Hugo</generator><language>en-US</language><copyright>2026 二重奏</copyright><lastBuildDate>Thu, 05 Nov 2026 10:31:50 +0000</lastBuildDate><atom:link href="https://deux.top/en/tags/cloudflare/index.xml" rel="self" type="application/rss+xml"/><item><title>Building an Automated Blog Translation Pipeline with Cloudflare Workers AI</title><link>https://deux.top/en/posts/cf-auto-translate/</link><pubDate>Thu, 05 Nov 2026 00:00:00 +0900</pubDate><guid>https://deux.top/en/posts/cf-auto-translate/</guid><description>Learn how to build a pipeline that automatically generates Japanese and English versions of your blog posts using Cloudflare Workers AI. This guide covers protecting Markdown syntax, optimizing costs, and ensuring translation quality.</description><content:encoded><![CDATA[<blockquote>
<p>This article was machine-translated by AI and may contain inaccuracies. See the <a href="/zh/posts/cf-auto-translate/">Chinese original</a> for the authoritative version.</p>
</blockquote>
<p>This blog is trilingual (Simplified Chinese / Japanese / English). Once I finish writing in Chinese and push it, the Japanese and English versions are automatically generated and deployed within minutes—with no manual translation involved in between. This article explains in plain language how it works and the pitfalls I encountered along the way.</p>
<h2 id="i-the-big-picture">I. The Big Picture</h2>
<p>The entire pipeline runs on GitHub Actions, using only free resources:</p>
<pre tabindex="0"><code>写完中文文章 push
  → GitHub Actions 触发翻译
    → 调 Cloudflare Workers AI（免费额度）
    → 译文先过几道自动检查
    → 检查通过，机器人提交回仓库
      → Netlify 自动部署，日/英文版上线
</code></pre><p>Before starting, I set four rules that proved to be lifesavers:</p>
<ul>
<li><strong>Translations go into git</strong>. The Japanese and English versions are real files in the repository, which can be opened, edited, or rolled back at any time. AI is just the &ldquo;first draft author,&rdquo; not the publishing channel.</li>
<li><strong>The visitor side is completely static</strong>. API keys exist only within GitHub Actions; there are zero keys and zero third-party scripts on the webpage, so readers won&rsquo;t use an extra watt of electricity on my account.</li>
<li><strong>Every call is billed</strong>. All requests pass through an AI Gateway, which logs every detail: how many tokens were used, how much it cost, and how long it took.</li>
<li><strong>Partial failures don&rsquo;t bring down the whole system</strong>. If the Japanese translation succeeds but the English one fails, the Japanese version goes live first, while the English version keeps the old version and waits for a retry.</li>
</ul>
<h2 id="ii-the-core-challenge-ai-can-ruin-markdown">II. The Core Challenge: AI Can Ruin Markdown</h2>
<p>Having AI translate plain text works quite well. But blog posts aren&rsquo;t just plain text—they are mixed with links, code blocks, tables, and list markers. In the early days, I would throw the entire Markdown file directly at the model, and the results often &ldquo;looked correct&rdquo; but were actually broken. Here is a real example:</p>
<pre tabindex="0"><code>原文：1. *感谢 [Hugo](https://gohugo.io) 提供代码支持*
翻后：[1. *Hugo](https://gohugo.io)に…（列表标记和星号被吞进了链接文字）
</code></pre><p>The worst part is that this kind of damage ** won&rsquo;t trigger an error **—the link remains the same, the page renders as usual, and you won&rsquo;t notice it unless you look closely. Simply writing &ldquo;Please maintain the format&rdquo; in the prompt is useless in practice: if the model can see the syntax, it can&rsquo;t help but mess with it.</p>
<p>The final solution I used was very direct: <strong>don&rsquo;t let the AI see the syntax</strong>.</p>
<p>Before translating, the program &ldquo;disassembles&rdquo; the article: code blocks, link addresses, inline code, and various markup symbols are all replaced with numbered placeholders like «0» and «1». All the AI sees are pure Chinese sentences plus a few numbers, so it has no way to break the syntax. After translation, the program swaps the placeholders back exactly as they were.</p>
<p>The disassembly uses a Markdown parser (remark) to locate elements via a syntax tree rather than using hard-coded regular expression matching—the parser understands all standard syntax, so no patches are needed when new syntax emerges. After translation, there are several automated checks:</p>
<ol>
<li><strong>Placeholder Integrity</strong>: Each placeholder must return exactly once; if any are missing, extra, or duplicated, it&rsquo;s considered a failure.</li>
<li><strong>Position Check</strong>: List markers must still be at the beginning of the line, and closing symbols must still be at the end of the line.</li>
<li><strong>Structural Comparison</strong>: The number of links, headings, and tables in the translation must match the original.</li>
</ol>
<p>If the checks fail, it automatically retries; if that still doesn&rsquo;t work, it splits the segment into smaller pieces and retries; if all else fails, it keeps the single line as-is—it&rsquo;s better to have one untranslated line than a broken entire article.</p>
<h2 id="iii-cost-saving-mechanism-translate-only-what-has-changed">III. Cost-saving mechanism: Translate only what has changed</h2>
<p>Each translation file carries two &ldquo;fingerprints&rdquo; (hashes): one for the body text and one for fields like titles and summaries:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="line"><span class="cl"><span class="nx">source_hash</span> <span class="p">=</span> <span class="s1">&#39;…&#39;</span>        <span class="c"># 正文的指纹</span>
</span></span><span class="line"><span class="cl"><span class="nx">source_fields_hash</span> <span class="p">=</span> <span class="s1">&#39;…&#39;</span> <span class="c"># 字段的指纹</span>
</span></span></code></pre></div><p>Before translating, the fingerprints are compared:</p>
<ul>
<li>changed ** body text ** → Only re-translate the body; the title and abstract remain as they were.</li>
<li>only changed ** abstract or tags ** → Only re-translate those fields; the body costs nothing.</li>
<li>nothing changed → zero calls (the scheduled reconciliation every Sunday at midnight performs a full scan like this; if everything is clean, it costs nothing).</li>
</ul>
<p>Additionally, Cloudflare&rsquo;s AI Gateway has a built-in 7-day cache: for the same request asked a second time, it returns the previous answer directly without charging. Therefore, pushing a fix for a typo often only costs the amount for the part that actually changed.</p>
<p>Real-world numbers: The free plan offers 10,000 neurons per day (Cloudflare&rsquo;s compute unit). Translating a 3,000-word article into one language takes about 50-160 neurons—you won&rsquo;t run out even if you translate over a dozen articles a day.</p>
<h2 id="iv-chinese-abstracts-are-also-managed-by-it">IV. Chinese abstracts are also managed by it</h2>
<p>Sometimes after writing an article, you forget to fill in the abstract. The pipeline detects that the Chinese article lacks an <code>description</code> and automatically writes a 70-160 word abstract based on the body text, writing it back to the article file. The abstracts for all three languages are translated from this source—** the three-language abstracts will always say the same thing **. Afterward, if you manually edit and save the abstract in the CMS, all three languages will update automatically.</p>
<h2 id="v-humans-always-have-the-final-say">V. Humans always have the final say</h2>
<p>The direction automation most easily goes astray is &ldquo;fully automated by machines, with no way for humans to intervene.&rdquo; This pipeline leaves a complete path for human intervention:</p>
<ul>
<li>Check <strong>&ldquo;Lock Translation&rdquo;</strong> for a specific translation in the CMS: This translation will henceforth be treated as a human-written draft, never overwritten or deleted, and will no longer sync even if the Chinese original is updated.</li>
<li>Check <strong>&ldquo;Do Not Translate&rdquo;</strong> for a Chinese article in the CMS: This article will exit the pipeline directly.</li>
<li>Proofread machine translations won&rsquo;t be blindly re-translated: As long as the corresponding part of the Chinese original hasn&rsquo;t changed, the fingerprint remains the same, so no money is spent on re-translation.</li>
<li>Automatic lifecycle synchronization: If a Chinese article is deleted, the translations are cleaned up accordingly; if a Chinese article is moved to drafts, the translations are taken down accordingly.</li>
</ul>
<h2 id="vi-pitfalls-encountered-if-you-want-to-build-one-too">VI. Pitfalls encountered (if you want to build one too)</h2>
<p>A few lessons learned through spending real money (and time):</p>
<ul>
<li>** Proactively adapt to rate limits **. Cloudflare&rsquo;s rate limiting is easier to trigger than what is stated in the documentation; having the client consistently slow down the request rate (one every 2 seconds) is much more hassle-free than handling backoff after the fact.</li>
<li><strong>Don&rsquo;t let the AI output the main body in JSON format</strong>. Stuffing Markdown into JSON causes the model to mangle it; the body text must be sent as plain text.</li>
<li><strong>When in doubt, err on the side of caution</strong>. If a translation is questionable, don&rsquo;t publish it; keep the old version live and wait for the next attempt—for a static blog, &ldquo;old&rdquo; is far less fatal than &ldquo;broken.&rdquo;</li>
<li><strong>Every line of defense must include offline self-testing</strong>. This system&rsquo;s validation logic consists of dozens of assertions; run them with every change for zero API cost.</li>
</ul>
<h2 id="vii-this-post-is-a-living-example">VII. This post is a living example.</h2>
<p>The Japanese and English versions you see now were automatically translated and deployed within minutes of this recent push. Use the language switcher at the top of the page to compare them—you&rsquo;ll be able to tell at a glance whether the AI&rsquo;s translation sounds natural.</p>
]]></content:encoded></item></channel></rss>