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
textstringThe text to be translated. This parameter must not be null, empty, or consist solely of whitespace.
languageLanguageThe target language represented as a member of the Language enumeration.
cancellationTokenCancellationTokenAn 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
textis null, empty, or contains no valid text characters.
TranslateAsync(ImageBuffer, Language, CancellationToken)
Asynchronously translates the content of an image into the target language. The model extracts visible text from the image and produces a translation.
public Task<string> TranslateAsync(ImageBuffer image, Language language, CancellationToken cancellationToken = default)
Parameters
imageImageBufferAn ImageBuffer containing the image whose text content should be translated. Cannot be
null.languageLanguageThe target language represented as a member of the Language enumeration.
cancellationTokenCancellationTokenAn 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.
Exceptions
- ArgumentNullException
Thrown if
imageisnull.- InvalidModelException
Thrown if the current language model does not support vision input required for image-based translation.
TranslateAsync(Attachment, Language, CancellationToken)
Asynchronously translates the content of an attachment into the target language. When the attachment contains an image, the model extracts visible text and produces a translation.
public Task<string> TranslateAsync(Attachment content, Language language, CancellationToken cancellationToken = default)
Parameters
contentAttachmentAn Attachment containing the content to be translated. Cannot be
null.languageLanguageThe target language represented as a member of the Language enumeration.
cancellationTokenCancellationTokenAn 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.
Exceptions
- ArgumentNullException
Thrown if
contentisnull.- InvalidModelException
Thrown if the current language model does not support vision input required for image-based translation.