Method TranslateAsync
- Namespace
- LMKit.Translation
- Assembly
- LM-Kit.NET.dll
TranslateAsync(string, Language, CancellationToken)
Asynchronously converts plain text into a specified target language while maintaining the original layout.
public Task<string> TranslateAsync(string text, Language language, CancellationToken cancellationToken = default)
Parameters
text
stringThe text to translate; cannot be
null
or whitespace.language
LanguageA member of the Language enumeration specifying the target language code for the translation.
cancellationToken
CancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
Examples
using LMKit.Translation;
using LMKit.Model;
using System;
using System.Threading.Tasks;
// Initialize the language model (LLM)
LLM languageModel = new LLM("path/to/your/model");
// Create an instance of TextTranslation using the LLM
TextTranslation textTranslation = new TextTranslation(languageModel);
// Text to translate
string text = "Welcome to the translation service.";
// Translate the text to Japanese asynchronously
string translatedText = await textTranslation.TranslateAsync(text, Language.Japanese);
Console.WriteLine(translatedText);
// Output: "翻訳サービスへようこそ。"
Remarks
This method is designed to handle text of any size, ensuring that the structure and formatting of the original content are preserved in the translation.
Exceptions
- ArgumentException
Thrown if the input
text
is null or whitespace.