Floryne Pierson
← All posts

15 languages, no framework, no server rendering

363 French pages, 15 languages, one committed JSON translation memory. Every localised page is a static HTML file generated at build time. The only runtime dependency is 73 lines of vanilla JavaScript.

Magical Chart started as an English-only site. Adding French was straightforward: translate the pages, deploy. The question was what to do with the second language, then the third. By the time German, Portuguese, Japanese, Arabic and a dozen others were in scope, the approach had to be systematic.

The constraint was the stack: plain HTML, no framework, no server. Everything that reached the CDN had to be a static file. The solution is a build-time HTML rewriter that takes an English source file and produces a fully translated, fully localized version for each target language.

1. The architecture: a build-time HTML rewrite

The generator reads an English HTML file and outputs one file per target language. It does not use a template language or a component system. It operates directly on the raw HTML string, splitting it into tokens and processing each one:

const split = s => s.split(
  /(<script\b[\s\S]*?<\/script\s*>|<style\b[\s\S]*?<\/style\s*>|<!--[\s\S]*?-->|<(?:[^"'<>]|"[^"]*"|'[^']*')+>)/gi
)

This splits the document into alternating text and tag tokens. Text tokens are translated. Tag tokens have their attributes translated and their href attributes rewritten to localised URLs. <script> blocks are parsed via the TypeScript AST to translate string literals without touching code.

2. Translation memory: a committed JSON file

Translations live in a committed JSON file in the repository. A build that runs with this file present does not make any network requests. It is deterministic and works offline.

Populating the file is a separate step. Missing keys are batched and sent to a machine translation API. For product pages where tone matters, a Claude call replaces the machine translation, with a system prompt that specifies brand names never to translate and the target register.

After translation, a small hand-authored overrides file replaces specific strings where the machine translation was wrong or off-tone. It is reviewed manually. Everything else runs automatically.

3. Two translation granularities

String-level translation works for most content: individual text nodes, button labels, alt text. But Romance languages have grammatical gender agreement that spans across inline elements. A sentence like "Your free reading is ready" may need the adjective to agree with a noun that is in a different tag. Translating the text node "free" in isolation produces the wrong form.

The fix is block-level translation: translate the entire paragraph as one unit, including its inline markup:

// Translate: "Your <strong>free</strong> reading is ready"
// Not:       "Your " + "free" + " reading is ready"

Block translations are stored in a separate committed file. The generator applies them after string-level translation, replacing matched elements with the pre-translated block versions. This matters most for Portuguese and Italian, where adjective-noun agreement is strict.

4. The 73-line runtime fallback

The build handles everything it can see. What it cannot see is content injected dynamically by JavaScript: AI-generated reading text, card names drawn at render time, date labels formatted by the browser. For those, a MutationObserver watches the DOM and translates content as it appears:

const observer = new MutationObserver(mutations => {
  for (const m of mutations) {
    for (const node of m.addedNodes) {
      if (node.nodeType === Node.TEXT_NODE) translateNode(node)
      else if (node.nodeType === Node.ELEMENT_NODE) translateSubtree(node)
    }
  }
})
observer.observe(document.body, { childList: true, subtree: true })

The runtime also rewrites fetch() to inject the target language into every AI reading request, so the model responds in the right language without each call site needing to know about localisation. It patches the Nominatim geocoder to send the correct accept-language header. It rewrites Stripe checkout links to inject the locale parameter.

The file is 73 lines. It has no imports and no external dependencies.

5. RTL, hreflang, and SEO at build time

Right-to-left languages (Arabic, Persian) get dir="rtl" on the <html> element, RTL-specific fonts injected via Google Fonts, and layout corrections applied inline. All of this happens at build time. No separate CSS file, no JavaScript layout adjustment.

Each generated page gets a full set of <link rel="alternate" hreflang="..."> tags pointing to every other language version, plus an updated canonical URL. The generator also writes or updates the language-specific sitemap and adds it to robots.txt. The English source page is updated simultaneously to include the new language in its own hreflang set.

The result: a new page added in English becomes available in French within one build. Adding a new language to the site requires running the generator once against all existing pages. The translation memory accumulates and subsequent builds are instant.