AI Text Translator — C# 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 C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// text-translator — AI text translator: prompt building + reply cleanup.
//
// Language: C# (netstandard2.0 surface — classic classes, no LINQ beyond
// the basics, so the snippet drops into any modern .NET target)
// 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.
//
// .NET strings are UTF-16, exactly like the TypeScript original, so
// string.Length and Substring reproduce the 12000-unit truncation verbatim,
// Regex runs the TS port's fence pattern unchanged, and a null return
// carries the "nothing survives" outcome, as in the TS port.
using System;
using System.Text.RegularExpressions;
namespace CosmoDev.Polyglot
{
/// <summary>One entry of the curated common-language list.</summary>
public sealed class Language
{
public readonly string Id;
public readonly string Label;
public Language(string id, string label)
{
Id = id;
Label = label;
}
}
/// <summary>One message for the provider layer; role is "system" or "user".</summary>
public sealed class ChatMessage
{
public readonly string Role;
public readonly string Content;
public ChatMessage(string role, string content)
{
Role = role;
Content = content;
}
}
/// <summary>AI text translator: prompt building + reply cleanup — pure logic, no I/O.</summary>
public static class Translator
{
/// <summary>Source-language sentinel: let the model detect the input language.</summary>
public const string AutoSource = "auto";
/// <summary>Sentinel entry shown in the "from" select.</summary>
public static readonly Language AutoLanguage = new Language(AutoSource, "Auto-detect");
/// <summary>Curated common-language list — the SSOT for both selects.</summary>
public static readonly Language[] Languages =
{
new Language("en", "English"), new Language("es", "Spanish"),
new Language("fr", "French"), new Language("de", "German"),
new Language("pt", "Portuguese"), new Language("it", "Italian"),
new Language("nl", "Dutch"), new Language("sv", "Swedish"),
new Language("pl", "Polish"), new Language("tr", "Turkish"),
new Language("ru", "Russian"), new Language("uk", "Ukrainian"),
new Language("ar", "Arabic"), new Language("fa", "Persian"),
new Language("he", "Hebrew"), new Language("hi", "Hindi"),
new Language("bn", "Bengali"), new Language("zh", "Chinese"),
new Language("ja", "Japanese"), new Language("ko", "Korean"),
new Language("vi", "Vietnamese"), new Language("th", "Thai"),
new Language("id", "Indonesian"), new Language("sw", "Swahili"),
};
/// <summary>Input cap; longer text is cut and marked so the model knows it ends early.</summary>
private const int MaxInputChars = 12000;
/// <summary>The TS port's /```[a-z]*\n?/ — stray markdown fences.</summary>
private static readonly Regex Fence = new Regex(@"```[a-z]*\n?");
/// <summary>Label for a known id; unknown ids pass through (the TS ?? id).</summary>
public static string LanguageLabel(string id)
{
foreach (Language l in Languages)
{
if (l.Id == id) return l.Label;
}
return id;
}
/// <summary>Build the [system, user] chat messages for the provider layer.</summary>
public static ChatMessage[] BuildPromptMessages(string text, string from, string to)
{
string fromPart = from == AutoSource
? "Detect the source language yourself."
: "The source language is " + LanguageLabel(from) + ".";
// string.Length is UTF-16 units — exactly the TS slice's unit.
string body = text.Length > MaxInputChars
? text.Substring(0, MaxInputChars) + "\n… (text truncated)"
: text;
string system = "You are a faithful translator. " + fromPart
+ " Translate the user's text into " + LanguageLabel(to)
+ ". Output ONLY the translation — no preamble, no notes, no backticks, "
+ "preserving paragraphs, formatting, and code blocks as-is.";
return new[]
{
new ChatMessage("system", system),
new ChatMessage("user", body),
};
}
/// <summary>
/// Clean the model's reply into displayable translation text. Strips
/// stray markdown fences and trims; null only when nothing survives
/// (the TS null).
/// </summary>
public static string ParseTranslation(string text)
{
string cleaned = Fence.Replace(text, "").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 →