/
Sample_MultipleInputs.cs
109 lines (96 loc) · 4.73 KB
/
Sample_MultipleInputs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using Azure.AI.Translation.Document.Tests;
using Azure.Core.TestFramework;
using NUnit.Framework;
namespace Azure.AI.Translation.Document.Samples
{
public partial class DocumentTranslationSamples : DocumentTranslationLiveTestBase
{
[Test]
[SyncOnly]
public void MultipleInputs()
{
#if SNIPPET
string endpoint = "<Document Translator Resource Endpoint>";
string apiKey = "<Document Translator Resource API Key>";
#else
string endpoint = TestEnvironment.Endpoint;
string apiKey = TestEnvironment.ApiKey;
#endif
var client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
#if SNIPPET
Uri source1SasUri = new Uri("<source1 SAS URI>");
Uri source2SasUri = new Uri("<source2 SAS URI>");
Uri frenchTargetSasUri = new Uri("<french target SAS URI>");
Uri arabicTargetSasUri = new Uri("<arabic target SAS URI>");
Uri spanishTargetSasUri = new Uri("<spanish target SAS URI>");
Uri frenchGlossarySasUri = new Uri("<french glossary SAS URI>");
#else
Uri source1SasUri = CreateSourceContainer(oneTestDocuments);
Uri source2SasUri = CreateSourceContainer(new List<TestDocument> { new TestDocument("Document2.txt", "Second english test document") });
Uri frenchTargetSasUri = CreateTargetContainer();
Uri arabicTargetSasUri = CreateTargetContainer();
Uri spanishTargetSasUri = CreateTargetContainer();
var glossary = new TestDocument("glossary.tsv", "test\tglossarytest");
Uri frenchGlossarySasUri = CreateGlossary(glossary);
#endif
var glossaryFormat = "TSV";
var input1 = new DocumentTranslationInput(source1SasUri, frenchTargetSasUri, "fr", new TranslationGlossary(frenchGlossarySasUri, glossaryFormat));
input1.AddTarget(spanishTargetSasUri, "es");
var input2 = new DocumentTranslationInput(source2SasUri, arabicTargetSasUri, "ar");
input2.AddTarget(frenchTargetSasUri, "fr", new TranslationGlossary(frenchGlossarySasUri, glossaryFormat));
var inputs = new List<DocumentTranslationInput>()
{
input1,
input2
};
DocumentTranslationOperation operation = client.StartTranslation(inputs);
TimeSpan pollingInterval = new(1000);
while (true)
{
operation.UpdateStatus();
Console.WriteLine($" Status: {operation.Status}");
Console.WriteLine($" Created on: {operation.CreatedOn}");
Console.WriteLine($" Last modified: {operation.LastModified}");
Console.WriteLine($" Total documents: {operation.DocumentsTotal}");
Console.WriteLine($" Succeeded: {operation.DocumentsSucceeded}");
Console.WriteLine($" Failed: {operation.DocumentsFailed}");
Console.WriteLine($" In Progress: {operation.DocumentsInProgress}");
Console.WriteLine($" Not started: {operation.DocumentsNotStarted}");
if (operation.HasCompleted)
{
break;
}
else
{
if (operation.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
{
pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
}
Thread.Sleep(pollingInterval);
}
}
foreach (DocumentStatusResult document in operation.GetValues())
{
Console.WriteLine($"Document with Id: {document.Id}");
Console.WriteLine($" Status:{document.Status}");
if (document.Status == DocumentTranslationStatus.Succeeded)
{
Console.WriteLine($" Translated Document Uri: {document.TranslatedDocumentUri}");
Console.WriteLine($" Translated to language code: {document.TranslatedToLanguageCode}.");
Console.WriteLine($" Document source Uri: {document.SourceDocumentUri}");
}
else
{
Console.WriteLine($" Document source Uri: {document.SourceDocumentUri}");
Console.WriteLine($" Error Code: {document.Error.Code}");
Console.WriteLine($" Message: {document.Error.Message}");
}
}
}
}
}