Table of Contents

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

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

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 text is null or whitespace.