Method Translate
- Namespace
- LMKit.Translation
- Assembly
- LM-Kit.NET.dll
Translate(string, Language, CancellationToken)
Translates the specified text into the target language synchronously while preserving the original layout.
public string Translate(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
- string
A string containing the translated text with formatting and layout preserved.
Examples
using LMKit.Model;
using LMKit.Translation;
using LMKit.TextGeneration;
using System;
// 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 = "This is a sample text to be translated.";
// Translate the text to German
string translatedText = translator.Translate(text, Language.German);
Console.WriteLine($"Original: {text}");
Console.WriteLine($"Translated: {translatedText}");
Remarks
This method synchronously wraps the asynchronous translation process. It partitions large texts into chunks, translates each chunk individually, and then reassembles the final translated text.
Exceptions
- ArgumentException
Thrown if the input
textis null or whitespace.
Translate(ImageBuffer, Language, CancellationToken)
Translates the content of an image into the target language synchronously. The model extracts visible text from the image and produces a translation.
public string Translate(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
- string
A string containing the translated text extracted from the image.
Exceptions
- ArgumentNullException
Thrown if
imageisnull.- InvalidModelException
Thrown if the current language model does not support vision input required for image-based translation.
Translate(Attachment, Language, CancellationToken)
Translates the content of an attachment into the target language synchronously. When the attachment contains an image, the model extracts visible text and produces a translation.
public string Translate(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
- string
A string containing the translated text.
Exceptions
- ArgumentNullException
Thrown if
contentisnull.- InvalidModelException
Thrown if the current language model does not support vision input required for image-based translation.