loading
Warning:
One model per language pair A translator is created for a pair, and each pair is its own downloadable model: a runtime that can do en → fr may not be able to do fr → ja. Probe the pair you actually need. Chromium only; see LanguageModel for the rest.

Detect the language

LanguageDetector.Create / Detect

Ranks the candidate languages, most confident first. A result of 'und' is the detector saying it could not decide - short input often lands there. This is the natural front half of translation.

C#
@inject Bit.Butil.LanguageDetector languageDetector

await using var detector = await languageDetector.Create();
var candidates = await detector.Detect(text);
var best = candidates.FirstOrDefault()?.DetectedLanguage;
Live sample
Text
detection output
Results will appear here when you interact with the samples.

Can this pair be translated?

Translator.IsSupported / Availability

Availability takes the pair, either as a TranslatorOptions or as two BCP 47 tags. Downloadable means creating a session will fetch that pair's model first.

C#
var availability = await translator.Availability("en", "fr");

if (availability == AiAvailability.Downloadable)
{
    // creating the session will download - do it from a user gesture, show progress
}
Live sample
availability output
Results will appear here when you interact with the samples.

Translate

Translator.Create / Translate / TranslateStreaming

Create a session for the pair, then translate through it as many times as you like. TranslateStreaming reports the translation as it is produced, which is worth it for anything longer than a sentence. Dispose the session when the pair is no longer needed.

C#
await using var session = await translator.Create("en", "fr",
    onDownloadProgress: p => { _progress = p; InvokeAsync(StateHasChanged); });

var translated = await session!.Translate(text);
Live sample
translation output
Results will appear here when you interact with the samples.

Detect, then translate

LanguageDetector + Translator

The two together are what 'translate this page for me' actually is: detect the source, create a translator into the user's own language, and skip the whole thing when they already match.

C#
await using var detector = await languageDetector.Create();
var detected = (await detector.Detect(text)).FirstOrDefault()?.DetectedLanguage;

if (detected is null or "und" || detected == userLanguage) return text;

await using var session = await translator.Create(detected, userLanguage);
return await session!.Translate(text) ?? text;

API reference

Member
Signature
Description
Translator.IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes the Translator API.
Translator.Availability
ValueTask<AiAvailability> Availability(TranslatorOptions options) / Availability(string sourceLanguage, string targetLanguage)
Whether this language pair can be translated right now, and whether that means a download first.
Translator.Create
ValueTask<TranslatorSession?> Create(TranslatorOptions options, Action<double>? onDownloadProgress = null) / Create(string sourceLanguage, string targetLanguage, …)
Creates a translator for one language pair. Null when the pair can't be served.
TranslatorSession.Translate
ValueTask<string?> Translate(string input)
Translates a piece of text and waits for the whole result.
TranslatorSession.TranslateStreaming
Task<string> TranslateStreaming(string input, Action<string>? onChunk = null)
Translates a piece of text, reporting it as it is produced.
TranslatorSession.DisposeAsync
ValueTask DisposeAsync()
Destroys the model instance and frees what it holds. Always dispose.
LanguageDetector.IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes the LanguageDetector API.
LanguageDetector.Availability
ValueTask<AiAvailability> Availability() / Availability(LanguageDetectorOptions options)
Whether a detector can be created right now, and whether that means a download first.
LanguageDetector.Create
ValueTask<LanguageDetectorSession?> Create(LanguageDetectorOptions? options = null, Action<double>? onDownloadProgress = null)
Creates a detector. Null when the runtime refused.
LanguageDetectorSession.Detect
ValueTask<LanguageDetectionResult[]> Detect(string input)
Ranks the languages a piece of text might be in, most confident first.
LanguageDetectorSession.DisposeAsync
ValueTask DisposeAsync()
Destroys the model instance and frees what it holds. Always dispose.
An unhandled error has occurred. Reload 🗙