Skip to content

Commit 1235e9f

Browse files
HavenDVclaude
andcommitted
feat: add document translation + voice streaming examples, update CLAUDE.md
- TranslateDocument.cs: upload-poll-download workflow example - VoiceStreaming.cs: REST setup endpoint for real-time WebSocket translation - CLAUDE.md: document voice API findings (not ISpeechToTextClient compatible) and document translation workflow notes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cf301ec commit 1235e9f

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

CLAUDE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,19 @@ DeepL API has tagged operations generating sub-clients:
4848
- `client.StyleRules.*` — Style rule management (v3)
4949
- `client.VoiceAPI.*` — Voice streaming (v3)
5050
- `client.AdminApi.*` — Developer key management
51+
52+
## Voice API Notes
53+
54+
The Voice API provides **real-time speech translation** via WebSocket:
55+
1. REST endpoint (`GetVoiceStreamingUrlAsync`) returns an ephemeral `wss://` URL + token
56+
2. Client connects to WebSocket and streams audio bytes
57+
3. Server returns transcription + multi-language translations in real-time
58+
59+
**Not ISpeechToTextClient compatible** — the bi-directional WebSocket streaming pattern doesn't map to MEAI's single-request transcription model. Use the SDK's native `client.VoiceAPI.GetVoiceStreamingUrlAsync()` + a WebSocket client.
60+
61+
## Document Translation Workflow
62+
63+
Three-step async pattern:
64+
1. **Upload:** `client.TranslateDocuments.TranslateDocumentAsync(targetLang, fileBytes, filename)` → returns `DocumentId` + `DocumentKey`
65+
2. **Poll:** `client.TranslateDocuments.GetDocumentStatusAsync(docId, docKey)` → check `Status` (Queued → Translating → Done)
66+
3. **Download:** `client.TranslateDocuments.DownloadDocumentAsync(docId, docKey)` → returns `byte[]` (one-time download)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
order: 50
3+
title: Translate Document
4+
slug: translate-document
5+
6+
Shows how to translate a document using the upload-poll-download workflow.
7+
*/
8+
9+
namespace DeepL.IntegrationTests;
10+
11+
public partial class Tests
12+
{
13+
[TestMethod]
14+
public async Task TranslateDocument()
15+
{
16+
using var client = GetAuthenticatedClient();
17+
18+
//// DeepL's document translation follows a three-step workflow:
19+
//// 1. Upload the document and specify the target language
20+
//// 2. Poll for translation status until complete
21+
//// 3. Download the translated document
22+
23+
//// Step 1: Upload a text file for translation.
24+
var content = "Hello, world! This is a test document for translation."u8.ToArray();
25+
var uploadResponse = await client.TranslateDocuments.TranslateDocumentAsync(
26+
targetLang: TargetLanguage.De,
27+
file: content,
28+
filename: "test.txt");
29+
30+
uploadResponse.DocumentId.Should().NotBeNullOrEmpty();
31+
uploadResponse.DocumentKey.Should().NotBeNullOrEmpty();
32+
33+
//// Step 2: Poll until the document translation is complete.
34+
GetDocumentStatusResponse status;
35+
do
36+
{
37+
await Task.Delay(1000);
38+
status = await client.TranslateDocuments.GetDocumentStatusAsync(
39+
documentId: uploadResponse.DocumentId!,
40+
documentKey1: uploadResponse.DocumentKey!);
41+
}
42+
while (status.Status is GetDocumentStatusResponseStatus.Queued
43+
or GetDocumentStatusResponseStatus.Translating);
44+
45+
status.Status.Should().Be(GetDocumentStatusResponseStatus.Done);
46+
47+
//// Step 3: Download the translated document (one-time download).
48+
var translatedBytes = await client.TranslateDocuments.DownloadDocumentAsync(
49+
documentId: uploadResponse.DocumentId!,
50+
documentKey1: uploadResponse.DocumentKey!);
51+
52+
translatedBytes.Should().NotBeEmpty();
53+
var translatedText = System.Text.Encoding.UTF8.GetString(translatedBytes);
54+
translatedText.Should().NotBeNullOrEmpty();
55+
}
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
order: 60
3+
title: Voice Streaming
4+
slug: voice-streaming
5+
6+
Shows how to initiate a voice streaming session for real-time translation.
7+
*/
8+
9+
namespace DeepL.IntegrationTests;
10+
11+
public partial class Tests
12+
{
13+
[TestMethod]
14+
public async Task VoiceStreaming()
15+
{
16+
using var client = GetAuthenticatedClient();
17+
18+
//// The DeepL Voice API provides real-time speech translation via WebSocket.
19+
//// Step 1: Call the REST endpoint to get an ephemeral WebSocket URL and token.
20+
var response = await client.VoiceAPI.GetVoiceStreamingUrlAsync(
21+
request: new GetVoiceStreamingUrlRequest
22+
{
23+
SourceMediaContentType = VoiceMediaContentType.AudioPcm_Encoding_s16le_Rate_16000,
24+
SourceLanguage = VoiceSourceLanguage.En,
25+
TargetLanguages = ["de"],
26+
});
27+
28+
response.StreamingUrl.Should().NotBeNullOrEmpty();
29+
response.Token.Should().NotBeNullOrEmpty();
30+
31+
//// Step 2: Connect to the WebSocket URL (`response.StreamingUrl`)
32+
//// and stream audio bytes for real-time transcription and translation.
33+
//// This example only verifies the REST setup endpoint;
34+
//// actual WebSocket streaming requires a separate WebSocket client.
35+
}
36+
}

0 commit comments

Comments
 (0)