ADR-310: Universal Inbox Language Translation
Status: Phase 1 Implemented — PR #434 Date: 2026-08-09 · Decider: Eugin Song
Status: Phase 1 Implemented — PR #434 Date: 2026-08-09 · Decider: Eugin Song
Related: ADR-280 (inbox labels) · ADR-170/V152 (lang_preferred) · ADR-301 (email consolidation) · ADR-302 (multitenant inbound)
A ELI5 — Why This Matters
Contacts message RAIA (Real estate Artificial Intelligence Agent) agents in Thai, Arabic, Mandarin, French, and other languages. Before this ADR, agents who don't speak those languages simply couldn't read the messages — contacts were ignored or mishandled with no way to tell if it was a language problem or a deliberate non-response.
ADR-310 adds a translation pill beneath every non-English inbound message in the Universal Inbox. The original text stays visible (always); clicking the pill reveals an English translation. The signal to show the pill is already in the database — tbl_persons.lang_preferred has always stored the contact's language preference. This ADR just makes the inbox UI act on it.
Phase 1 is free: Google Translate free tier, zero DB migrations, zero new infra. Phase 2 upgrades to DeepL (best-in-class quality for Thai/Arabic/Mandarin) and adds a Translate & Send button so agents can reply in the contact's language.
B User Guide — The Translation Pill
When a contact messages in a non-English language, the inbox thread renders two blocks beneath the sender's name:
- Original text — always shown first, exactly as received
- Translation pill — collapsed by default, showing
🌐 Translated from Thai ▾
┌─────────────────────────────────────────────────────┐ │ EuGin K. · inbound · 14:32 │ │ ┌─────────────────────────────────────────────────┐│ │ │ สวัสดีครับ ผมสนใจห้องพักของคุณครับ... ││ │ └─────────────────────────────────────────────────┘│ │ ┌──────────────────────────────────────────────── ││ │ │ 🌐 Translated from Thai ▾ ││ │ │ Hello, I am interested in your room... ││ │ └──────────────────────────────────────────────── ││ └─────────────────────────────────────────────────────┘
Show / Hide Translation
-
1Pill is collapsed by default
The pill reads
🌐 Translated from [Language] ▾. The original text is always visible above it. -
2Click to expand
Clicking the pill reveals the English translation. The pill chevron flips to ▴. Click again to collapse.
-
3Translation unavailable
If the API fails, the pill shows
⚠ Translation unavailable. The original message is unaffected — the thread never breaks. -
4English messages — no pill
If the detected language is English and
lang_preferredis English or null, no pill appears.
Phase 2 — Translate & Send (Composer)
The translate-and-send flow is defined in ADR-310 but not implemented in Phase 1 (PR #434). It will appear in a future sprint.
When conversation.lang_preferred !== 'en' (or person.lang_preferred !== 'en'), a second button appears in the reply composer next to Send:
[ Reply as normal ] [ Translate to Thai (EuGin's preference) → ]
Clicking the button opens a preview modal showing the translated reply. The agent can edit the preview before confirming. Only the translated text is sent — the English draft is preserved in the UI.
C Developer Guide — API Route
A new Next.js API route handles all translation requests:
POST /api/inbox/translate
Request
{
text: string; // Original message text
source_lang?: string; // ISO 639-1 (e.g. 'th', 'ar') — auto-detect if omitted
target_lang: string; // Always 'en' for inbound display (Phase 1)
message_id?: string; // UUID — for Phase 2 caching
}
Response
{
translated_text: string;
detected_source_lang: string; // ISO 639-1 code from provider
provider: 'google' | 'deepl' | 'libretranslate';
cached: boolean; // true if served from translated_content (Phase 2)
}
Error Response (graceful degradation)
{
error: 'translation_unavailable';
message: string; // Human-readable; NOT shown to ops agent
}
The FE catches translation errors silently — no toast, no broken UI. The original message is always shown. A failed translation shows the ⚠ Translation unavailable pill state.
Translation Trigger Logic
Translation is triggered on two independent conditions (OR logic):
const shouldTranslate = (message: ConversationMessage, person: Person): boolean => {
const detectedLang = message.detected_language ?? null;
const preferredLang = person.lang_preferred ?? 'en';
// Trigger A: message language detected as non-English
if (detectedLang && detectedLang !== 'en') return true;
// Trigger B: contact's preferred language is non-English
// (covers cases where detection fails or returns null)
if (preferredLang !== 'en') return true;
return false;
};
When NOT to translate
| Condition | Reason |
|---|---|
Direction is outbound | Agent-authored messages are never auto-translated for display |
Detected lang = 'en' AND lang_preferred = 'en' or null | Already English |
| Message content is empty or < 3 characters | Too short to translate meaningfully |
translated_content already cached | Phase 2 — avoid repeat API calls |
Language detection approach
- Primary: Use the translation provider's built-in detect endpoint (Google
detectLanguage, DeepLsource_lang: nullauto-detect). One API call for detect + translate together. - Fallback: If provider does not return a detected language, fall back to
person.lang_preferredas the source language label. - Phase 1: No separate
langdetectPython library — stack is purely TypeScript/Node.
Data Model
Translation is transient (computed on-read). Zero schema migrations. The /context API route already returns person.lang_preferred — the FE uses this to decide whether to show the pill.
A nullable translated_content TEXT column is added to tbl_conversation_messages. Written on first successful translation; served from cache on subsequent loads.
Phase 2 migration (DDL)
-- Phase 2 — migration number assigned from origin/main at implementation time
ALTER TABLE tbl_conversation_messages
ADD COLUMN IF NOT EXISTS translated_content TEXT DEFAULT NULL;
COMMENT ON COLUMN tbl_conversation_messages.translated_content IS
'Cached English translation of non-English inbound messages. NULL = not yet
translated or source is English. Populated by /api/inbox/translate on first
successful translation. Not persisted for outbound messages.';
Schema summary
| Column | Table | Action | Phase |
|---|---|---|---|
translated_content TEXT nullable | tbl_conversation_messages | ADD (new) | Phase 2 |
lang_preferred | tbl_persons | Read-only — already exists | Phase 1 |
lang_preferred | tbl_conversations | Read-only — already exists | Phase 1 |
Provider Selection
| Provider | Quality | Cost | Phase |
|---|---|---|---|
| Google Translate API | ⭐⭐⭐⭐ Very good, widest language coverage | Free tier: 500K chars/mo; ~$20/1M chars after | Phase 1 |
| DeepL Pro API | ⭐⭐⭐⭐⭐ Best-in-class for Thai, Arabic, Mandarin, French | ~$5.49/mo for 500K chars (Pro) | Phase 2 |
| LibreTranslate | ⭐⭐⭐ Acceptable; lower quality on Asian languages | Free; self-hosted compute cost | Local dev fallback |
Phase 1: Google Translate free tier. Zero cost for expected dev/staging volume.
Phase 2: DeepL Pro API. Superior quality for the primary use cases: Thai, Arabic, Mandarin.
Local dev: LibreTranslate via Docker (libretranslate/libretranslate) — set TRANSLATION_PROVIDER=libretranslate in .env.local.
Phase 1 vs Phase 2
| Feature | Phase 1 (Shipped — PR #434) | Phase 2 (Planned) |
|---|---|---|
| Inbound display translation | ✅ Yes | ✅ Yes (from cache) |
| Translation provider | Google Translate (free tier) | DeepL Pro |
| DB migration | ❌ None | ADD COLUMN translated_content |
| Translation caching | ❌ Transient (recomputed each load) | ✅ Written to translated_content |
| Outbound Translate & Send | ❌ Not in scope | ✅ Preview modal + confirm send |
| Reply composer button | ❌ Not rendered | ✅ When lang_preferred ≠ 'en' |
Environment Variables
# .env.local / Vercel env
TRANSLATION_PROVIDER=google # google | deepl | libretranslate
GOOGLE_TRANSLATE_API_KEY=... # Phase 1
DEEPL_API_KEY=... # Phase 2 production
LIBRETRANSLATE_URL=http://localhost:5000 # Local dev fallback
Component Design
MessageTranslationPill.tsx
New component: portal/src/components/inbox/MessageTranslationPill.tsx
interface MessageTranslationPillProps {
messageId: string;
originalText: string;
sourceLang: string; // e.g. 'th'
sourceLangLabel: string; // e.g. 'Thai'
prefetchedTranslation?: string; // Phase 2: from translated_content cache
}
| State | Renders |
|---|---|
| Collapsed (default) | 🌐 Translated from Thai ▾ — clickable pill |
| Expanded | English translation text beneath pill; chevron flips to ▴ |
| Loading | Skeleton shimmer while /api/inbox/translate is in-flight |
| Error | ⚠ Translation unavailable — no retry, fail silently |
Accessibility: aria-expanded, aria-label="Show translation" on the pill button.
Files to create / modify (Phase 1)
| File | Action |
|---|---|
portal/src/app/api/inbox/translate/route.ts | New — translation API route |
portal/src/components/inbox/MessageTranslationPill.tsx | New — UI pill component |
portal/src/components/inbox/MessageBubble.tsx | Modify — render pill beneath inbound messages |
portal/src/lib/translation.ts | New — provider abstraction (Google / DeepL / LibreTranslate) |
.env.local / .env.example | Add translation env vars |
D References & External Sources
Related ADRs & Specs
| Source | Type | Notes |
|---|---|---|
| ADR-273/280/285/286 — Universal Inbox Core Ops | Internal ADR | Labels SSOT, audience & chip palette that this translation feature extends |
| ADR-280 — Inbox Mechanical Labels | Internal ADR | The label system that governs how pills and signals appear in the inbox thread |
| ADR-170 / V152 — comm_preferred / lang_preferred | Internal ADR | The original schema decision that added lang_preferred to tbl_persons |
| ADR-302 — Multitenant Inbound | Internal ADR | The inbound email routing architecture that this translation layer hooks into |
| Google Cloud Translation API docs | External | Phase 1 provider — free tier 500K chars/mo; detectLanguage endpoint used for auto-detect |
| DeepL Pro API | External | Phase 2 production provider — best-in-class for Thai, Arabic, Mandarin |
| LibreTranslate | External | Local dev fallback — self-hosted via Docker; no API key needed |