AI Text Translator — TypeScript 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 TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
// src/lib/translate.ts — the pure core of the AI text translator
// (AI Studio tool #3, FEAT-075; class rollout per the FEAT-049 verdict).
// Language list + prompt building + reply cleanup are pure so the island is
// a thin shell over the shared provider layer (src/lib/ai-studio), and this
// file is a future Go-twin candidate once the class proves out.
export interface Language {
id: string;
label: string;
}
/** Source-language sentinel: let the model detect the input language. */
export const AUTO_SOURCE = 'auto';
export const AUTO_LANGUAGE: Language = { id: AUTO_SOURCE, label: 'Auto-detect' };
/** Curated common-language list — the SSOT for both island selects. */
export const LANGUAGES: Language[] = [
{ 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' },
];
const MAX_INPUT_CHARS = 12000;
function languageLabel(id: string): string {
return LANGUAGES.find((l) => l.id === id)?.label ?? id;
}
/** Build the [system, user] chat messages for the provider layer. */
export function buildPromptMessages(
text: string,
opts: { from: string; to: string },
): Array<{ role: 'system' | 'user'; content: string }> {
const fromPart =
opts.from === AUTO_SOURCE
? 'Detect the source language yourself.'
: `The source language is ${languageLabel(opts.from)}.`;
const truncated = text.length > MAX_INPUT_CHARS;
const body = truncated ? `${text.slice(0, MAX_INPUT_CHARS)}\n… (text truncated)` : text;
return [
{
role: 'system',
content: `You are a faithful translator. ${fromPart} Translate the user's text into ${languageLabel(
opts.to,
)}. Output ONLY the translation — no preamble, no notes, no backticks, preserving paragraphs, formatting, and code blocks as-is.`,
},
{ role: 'user', content: body },
];
}
/**
* Clean the model's reply into displayable translation text. No strict
* format contract — any non-empty cleaned text is the translation; null
* only when nothing survives.
*/
export function parseTranslation(text: string): string | null {
const cleaned = text.replace(/```[a-z]*\n?/g, '').trim();
return cleaned.length > 0 ? cleaned : null;
}
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 →