RAIA™ Specification

ADR-310: Universal Inbox Language Translation

Status: Phase 1 Implemented — PR #434  Date: 2026-08-09  ·  Decider: Eugin Song

← Back to Developer Hub TEXTBOOK SPECIFICATION

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)

Phase 1
Live — Inbound display translation
LOW
Risk tier — read-path only, no data mutated
0
DB migrations in Phase 1
Phase 2
DeepL + cache column + translate-and-send

A ELI5 — Why This Matters

Inbound Message (Thai / Arabic…) Language Detection Google / DeepL Translation Cache / API translated_content Translation Pill UI 🌐 Click to expand Step 1 Step 2 Step 3 Step 4 ADR-310 Translation Pipeline
Plain English

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:

  1. Original text — always shown first, exactly as received
  2. Translation pill — collapsed by default, showing 🌐 Translated from Thai ▾
What agents see
┌─────────────────────────────────────────────────────┐
│  EuGin K.  · inbound · 14:32                        │
│  ┌─────────────────────────────────────────────────┐│
│  │  สวัสดีครับ ผมสนใจห้องพักของคุณครับ...        ││
│  └─────────────────────────────────────────────────┘│
│  ┌────────────────────────────────────────────────  ││
│  │  🌐 Translated from Thai ▾                      ││
│  │  Hello, I am interested in your room...         ││
│  └────────────────────────────────────────────────  ││
└─────────────────────────────────────────────────────┘

Show / Hide Translation

  • 1
    Pill is collapsed by default

    The pill reads 🌐 Translated from [Language] ▾. The original text is always visible above it.

  • 2
    Click to expand

    Clicking the pill reveals the English translation. The pill chevron flips to ▴. Click again to collapse.

  • 3
    Translation unavailable

    If the API fails, the pill shows ⚠ Translation unavailable. The original message is unaffected — the thread never breaks.

  • 4
    English messages — no pill

    If the detected language is English and lang_preferred is English or null, no pill appears.

Phase 2 — Translate & Send (Composer)

Phase 2 — Not Yet Shipped

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.


Component Architecture — Translation System Frontend (Portal) MessageBubble.tsx MessageTranslationPill.tsx translation.ts provider abstraction POST API Route /api/inbox/translate route.ts Google Translate Phase 1 DeepL Pro Phase 2 production tbl_conversations translated_content cache

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
}
Graceful degradation rule

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

ConditionReason
Direction is outboundAgent-authored messages are never auto-translated for display
Detected lang = 'en' AND lang_preferred = 'en' or nullAlready English
Message content is empty or < 3 charactersToo short to translate meaningfully
translated_content already cachedPhase 2 — avoid repeat API calls

Language detection approach

  • Primary: Use the translation provider's built-in detect endpoint (Google detectLanguage, DeepL source_lang: null auto-detect). One API call for detect + translate together.
  • Fallback: If provider does not return a detected language, fall back to person.lang_preferred as the source language label.
  • Phase 1: No separate langdetect Python library — stack is purely TypeScript/Node.

Data Model

Phase 1 — No DB changes

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.

Phase 2 — Cache column

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

ColumnTableActionPhase
translated_content TEXT nullabletbl_conversation_messagesADD (new)Phase 2
lang_preferredtbl_personsRead-only — already existsPhase 1
lang_preferredtbl_conversationsRead-only — already existsPhase 1

Provider Selection

ProviderQualityCostPhase
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
Provider decision

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

FeaturePhase 1 (Shipped — PR #434)Phase 2 (Planned)
Inbound display translation✅ Yes✅ Yes (from cache)
Translation providerGoogle Translate (free tier)DeepL Pro
DB migration❌ NoneADD 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
}
StateRenders
Collapsed (default)🌐 Translated from Thai ▾ — clickable pill
ExpandedEnglish translation text beneath pill; chevron flips to ▴
LoadingSkeleton 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)

FileAction
portal/src/app/api/inbox/translate/route.tsNew — translation API route
portal/src/components/inbox/MessageTranslationPill.tsxNew — UI pill component
portal/src/components/inbox/MessageBubble.tsxModify — render pill beneath inbound messages
portal/src/lib/translation.tsNew — provider abstraction (Google / DeepL / LibreTranslate)
.env.local / .env.exampleAdd translation env vars

D References & External Sources

Related ADRs & Specs

SourceTypeNotes
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