Back to Devexpress

IAIDocProcessingService.TranslateAsync(DocumentRange, CultureInfo, CancellationToken) Method

officefileapi-devexpress-dot-aiintegration-dot-docs-dot-iaidocprocessingservice-dot-translateasync-x28-documentrange-cultureinfo-cancellationtoken-x29.md

latest5.9 KB
Original Source

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

csharp
Task TranslateAsync(
    DocumentRange documentRange,
    CultureInfo culture,
    CancellationToken cancellationToken = default(CancellationToken)
)
vb
Function TranslateAsync(
    documentRange As DocumentRange,
    culture As CultureInfo,
    cancellationToken As CancellationToken = Nothing
) As Task

Parameters

NameTypeDescription
documentRangeDocumentRange

The document range which content should be translated.

| | culture | CultureInfo |

An object that specifies culture settings applied during translation.

|

Optional Parameters

NameTypeDefaultDescription
cancellationTokenCancellationTokennull

The token that cancels the task.

|

Returns

TypeDescription
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:

csharp
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);
}
vb
Imports DevExpress.AIIntegration
Imports DevExpress.AIIntegration.Docs
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports Microsoft.Extensions.AI
Imports System.Globalization
Imports System.IO
Imports System.Threading.Tasks

Module Module1
    Async Function Main() As Task

        ' See "Register AI extension service" section for implementation code
        var docProcessingService = 
            defaultAIExtensionsContainer.CreateAIDocProcessingService()

        Using wordProcessor As New RichEditDocumentServer()
            Dim fs As FileStream = File.OpenRead(
                Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory, 
                    "Documents/FirstLookShortened.docx"))
            wordProcessor.LoadDocument(fs)
            fs.Close()

            Dim paragraph As Paragraph = wordProcessor.Document.Paragraphs(1)
            Await docProcessingService.TranslateAsync(
                paragraph.Range, 
                New CultureInfo("DE-DE"))

            ' Save the modified document
            Dim outputFilePath As String = Path.Combine(Environment.CurrentDirectory, "Document1_translated.docx")
            wordProcessor.SaveDocument(outputFilePath, DocumentFormat.Docx)
        End Using
    End Function
End Module

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.

office-file-api-ai-implementation/CS/Controllers/TranslateController.cs#L76

csharp
var pageRange = wordProcessor.Document.CreateRange(fixedRange.Start, fixedRange.Length); // Translate the first page
await docProcessingService.TranslateAsync(pageRange, language);
break;

See Also

IAIDocProcessingService Interface

IAIDocProcessingService Members

DevExpress.AIIntegration.Docs Namespace