Table of Contents

Method TranslateAsync

Namespace
LMKit.Translation
Assembly
LM-Kit.NET.dll

TranslateAsync(string, Language, CancellationToken)

Asynchronously translates the specified text into the target language while preserving its original formatting.

public Task<string> TranslateAsync(string text, Language language, CancellationToken cancellationToken = default)

Parameters

text string

The text to be translated. This parameter must not be null, empty, or consist solely of whitespace.

language Language

The target language represented as a member of the Language enumeration.

cancellationToken CancellationToken

An optional token that can be used to cancel the translation operation.

Returns

Task<string>

A task that represents the asynchronous translation operation. The task result is the translated text as a string.

Examples

using LMKit.Model;
using LMKit.Translation;
using LMKit.TextGeneration;
using System;
using System.Threading.Tasks;

async Task TranslateTextAsync()
{
    // Load the language model
    LM model = LM.LoadFromModelID("llama-3.2-1b");

    // Create the translator
    TextTranslation translator = new TextTranslation(model);

    // Text to be translated
    string text = "Welcome to the translation service.";

    // Translate the text to Japanese asynchronously
    string translatedText = await translator.TranslateAsync(text, Language.Japanese);

    Console.WriteLine($"Original: {text}");
    Console.WriteLine($"Translated: {translatedText}");
}

Remarks

The method handles texts of any length by partitioning them into manageable chunks, translating each chunk asynchronously, and then concatenating the results. It also retains the original text's structure and formatting.

Exceptions

ArgumentException

Thrown if text is null, empty, or contains no valid text characters.