Skip to content

AI Text Translator — Ruby source

Translate any text between 24 languages with an LLM — auto-detected or explicit source, straight from your browser with your own free Groq key.

This is the Ruby implementation — the same logic the interactive tool runs, in a shareable, citable form.

# text-translator — AI text translator: prompt building + reply cleanup.
#
# Language: Ruby (3.x, standard library only)
# Source:   CosmoDev polyglot showcase port of the `text-translator` tool,
#           ported from src/lib/translate.ts (the canonical implementation).
# License:  display source — part of CosmoDev's polyglot tool pages.
#
# The portable surface is pure — the curated language list, the [system,
# user] chat-message construction, and the reply cleanup — so plain hashes
# carry the Language / ChatMessage shapes and a bare gsub runs the TS port's
# fence regex unchanged. Ruby counts String#length in characters where the
# TS slice counts UTF-16 units; the cut-and-mark shape stays the same.

# frozen_string_literal: true

# Source-language sentinel: let the model detect the input language.
AUTO_SOURCE = 'auto'

# Sentinel entry shown in the "from" select.
AUTO_LANGUAGE = { id: AUTO_SOURCE, label: 'Auto-detect' }.freeze

# Curated common-language list — the SSOT for both selects.
LANGUAGES = [
  { id: 'en', label: 'English' },    { id: 'es', label: 'Spanish' },
  { id: 'fr', label: 'French' },     { id: 'de', label: 'German' },
  { id: 'pt', label: 'Portuguese' }, { id: 'it', label: 'Italian' },
  { id: 'nl', label: 'Dutch' },      { id: 'sv', label: 'Swedish' },
  { id: 'pl', label: 'Polish' },     { id: 'tr', label: 'Turkish' },
  { id: 'ru', label: 'Russian' },    { id: 'uk', label: 'Ukrainian' },
  { id: 'ar', label: 'Arabic' },     { id: 'fa', label: 'Persian' },
  { id: 'he', label: 'Hebrew' },     { id: 'hi', label: 'Hindi' },
  { id: 'bn', label: 'Bengali' },    { id: 'zh', label: 'Chinese' },
  { id: 'ja', label: 'Japanese' },   { id: 'ko', label: 'Korean' },
  { id: 'vi', label: 'Vietnamese' }, { id: 'th', label: 'Thai' },
  { id: 'id', label: 'Indonesian' }, { id: 'sw', label: 'Swahili' },
].freeze

# Input cap; longer text is cut and marked so the model knows it ends early.
MAX_INPUT_CHARS = 12_000

# Label for a known language id; unknown ids pass through (the TS `|| id`).
def language_label(id)
  hit = LANGUAGES.find { |l| l[:id] == id }
  hit ? hit[:label] : id
end

# Build the [system, user] chat messages for the provider layer.
def build_prompt_messages(text, from:, to:)
  from_part =
    if from == AUTO_SOURCE
      'Detect the source language yourself.'
    else
      "The source language is #{language_label(from)}."
    end
  body =
    if text.length > MAX_INPUT_CHARS
      "#{text[0, MAX_INPUT_CHARS]}\n… (text truncated)"
    else
      text
    end
  system = "You are a faithful translator. #{from_part} Translate the user's text " \
           "into #{language_label(to)}. Output ONLY the translation — no preamble, " \
           "no notes, no backticks, preserving paragraphs, formatting, and code blocks as-is."
  [
    { role: 'system', content: system },
    { role: 'user', content: body }
  ]
end

# Clean the model's reply into displayable translation text. Strips stray
# markdown fences (``` plus an optional lowercase info-string and newline)
# and trims; nil only when nothing survives — the TS port's null.
def parse_translation(text)
  cleaned = text.gsub(/```[a-z]*\n?/, '').strip
  cleaned.empty? ? nil : cleaned
end

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →