IAIDocProcessingService.TranslateAsync(DocumentRange, CultureInfo, CancellationToken) Method
Translates document content into a specific language with AI-powered translation services.
Namespace: DevExpress.AIIntegration.Docs
Assembly: DevExpress.AIIntegration.Docs.v25.2.dll
NuGet Package: DevExpress.AIIntegration.Docs
Declaration
Task TranslateAsync(
DocumentRange documentRange,
CultureInfo culture,
CancellationToken cancellationToken = default(CancellationToken)
)
Parameters
| Name | Type | Description |
|---|---|---|
| documentRange | DocumentRange | The document range which content should be translated. |
| culture | CultureInfo | An object that specifies culture settings applied during translation. |
Optional Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| cancellationToken | CancellationToken | null | The token that cancels the task. |
Returns
| Type | Description |
|---|---|
| Task | The response that contains AI-generated translation. |
Example
How to: Translate Second Document Paragraph
The following code snippet translates the second paragraph in a Word document:
using DevExpress.AIIntegration;
using DevExpress.AIIntegration.Docs;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using Microsoft.Extensions.AI;
// See "Register AI extension service" section for implementation code
var docProcessingService =
defaultAIExtensionsContainer.CreateAIDocProcessingService();
using (var wordProcessor = new RichEditDocumentServer()) {
FileStream fs = File.OpenRead(
Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Documents/FirstLookShortened.docx"));
wordProcessor.LoadDocument(fs);
fs.Close();
Paragraph paragraph = wordProcessor.Document.Paragraphs[1];
await docProcessingService.TranslateAsync(
paragraph.Range,
new System.Globalization.CultureInfo("DE-DE"));
// Save the modified document
string outputFilePath =
Path.Combine(Environment.CurrentDirectory, $"Document1_translated.docx");
wordProcessor.SaveDocument(outputFilePath, DocumentFormat.Docx);
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the TranslateAsync(DocumentRange, CultureInfo, CancellationToken) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.