ADR-045: Hybrid Three-Tier Translation (Static EN/ES, On-Device Browser AI, and Cached Gemini Fallback)
Context
The site is authored natively in English and Spanish (ADR-014) for editorial quality and canonical SEO (/ and /es). Visitors from other regions may still prefer reading in their own language. Pre-translating 20+ static locale bundles would bloat the bundle and drift across 45+ ADRs, while uncached server-side LLM translation would add latency, burn through the daily AI spend cap (ADR-033), and risk open-proxy abuse if arbitrary text were accepted.
Decision
Implement a progressive three-tier translation architecture gated by onDemandTranslation (FEATURE_ON_DEMAND_TRANSLATION, default-on):
1. Tier 0 (Native Static): Keep hand-crafted en and es strings, canonical URLs (/ and /es), and SSR metadata unchanged.
2. Tier 1 (On-Device Browser AI): On browsers supporting the Web Translator API (self.Translator — Desktop Chrome 138+, Desktop Edge 148+), translate chunks locally on the user device with zero server cost, masking Markdown code spans before translation.
3. Tier 2 (Server-Cached Vertex AI Fallback): For browsers without self.Translator (Safari, Firefox, mobile), POST /api/ai/translate translates via gemini-3.5-flash-lite backed by createTwoTierCache (in-memory L1 + Firestore L2, 30-day TTL) keyed by (model, lang, chunkId, contentHash).
4. Closed-World Chunk Security: The endpoint accepts only { lang, chunkId } against a curated BCP-47 allowlist and server-known content chunks (ui-chrome, portfolio, blog-index, blog:<slug>, adr-index, adr:<id>, arch:<componentId>, page:<pageId>) — never arbitrary text — eliminating open-proxy abuse. Cache hits refund the daily AI budget slot.
5. Privacy-First Language Suggestion: Detect candidate languages client-side via navigator.languages (no IP geolocation) and offer a dismissible one-line banner in the visitor’s language; clicking it provides the user gesture required for on-device pack download.
6. Canonical SEO Preservation: Dynamic languages render as a client-side overlay with an explicit source badge and one-click return to EN/ES.
Consequences
Benefits:
* Zero server or AI cost for en/es and Tier 1 on-device visitors.
* Amortized Tier 2 cost: only the first visitor per (lang, chunkId, contentHash) triggers Gemini Flash-Lite; all subsequent visitors get instant L1/L2 cache hits.
* Lazy per-section chunking avoids translating ~460 KB of ADRs and articles upfront.
* Zero privacy overhead — no GeoIP lookups or third-party translation cookies.
Trade-offs:
* First-time Tier 1 visitors wait briefly for the local language pack download; Tier 2 cache misses incur ~1–3s LLM latency.
* Browser Translator API support is currently desktop Chromium only; Safari, Firefox, and mobile rely on Tier 2.
* Existing inline language === 'en' ternaries must be migrated incrementally to shared chunks.
* Machine-translated views are intentionally excluded from search indexing.
Alternatives considered
1. Static build-time translation into 20+ languages: Rejected — inflates bundle size, causes translation drift, and risks SEO dilution.
2. Client-side WASM/WebGPU models (Bergamot / Transformers.js): Rejected — requires 30–100+ MB downloads per language pair, hurting Core Web Vitals.
3. Third-party translation widgets (Google Translate embed / Weglot): Rejected — breaks the design system, injects third-party scripts/cookies, and mangles technical terms and code blocks.
4. IP geolocation for language suggestion: Rejected — requires GeoIP lookups and guesses poorly for travelers and expats compared to navigator.languages.
5. Free-text translation endpoint: Rejected — opens an arbitrary-text proxy vector against the site’s Gemini quota.