Skip to content

Instantly share code, notes, and snippets.

@7effrey89
Created July 9, 2025 08:24
Show Gist options
  • Select an option

  • Save 7effrey89/bbc40c181e096f258237261e04aac50b to your computer and use it in GitHub Desktop.

Select an option

Save 7effrey89/bbc40c181e096f258237261e04aac50b to your computer and use it in GitHub Desktop.
C# Azure AI Translation - Batch Translate files
using Azure;
using Azure.AI.Translation.Document;
/// <summary>
/// This program demonstrates how to use Azure's Document Translation service to translate documents
/// stored in an Azure Blob Storage container from one language to another.
/// It authenticates using a custom endpoint and resource key, then submits a translation job,
/// waits for completion, and prints the status and results for each document.
/// https://learn.microsoft.com/en-us/azure/ai-services/translator/document-translation/how-to-guides/use-rest-api-programmatically?tabs=csharp
/// https://learn.microsoft.com/en-us/dotnet/api/overview/azure/ai.translation.document-readme?view=azure-dotnet
///
/// When sensitivity label is set on pdf files, it may not translate the document.
/// </summary>
class Program
{
// Connection information for the Document Translation service
private static readonly string endpoint = "https://<ai-foundry-resource>.cognitiveservices.azure.com/";
private static readonly string key = "xxxxxxxxxxxxxxxxxxxxxxxx";
// Connection information for the Azure Blob Storage containers, Input and Output folders
private static readonly string blobContainerSasUrl_Input = "https://<storage-account-blob-store-type>.blob.core.windows.net/<container-input>?sp=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static readonly string blobContainerSasUrl_Output = "https://<storage-account-blob-store-type>.blob.core.windows.net/<container-output>?sp=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// The target language code for translation.
private static readonly string targetLanguageCode = "en"; // "zh-Hans";
static async Task Main(string[] args)
{
//Blob container SAS URL
Uri sourceUri = new Uri(blobContainerSasUrl_Input);
//Blob container SAS URL
Uri targetUri = new Uri(blobContainerSasUrl_Output);
// initialize a new instance of the DocumentTranslationClient object to interact with the Document translation feature
DocumentTranslationClient client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(key));
// initialize a new instance of the `DocumentTranslationInput` object to provide the location of input for the translation operation
DocumentTranslationInput input = new DocumentTranslationInput(sourceUri, targetUri, targetLanguageCode);
// initialize a new instance of the DocumentTranslationOperation class to track the status of the translation operation
DocumentTranslationOperation operation = await client.StartTranslationAsync(input);
await operation.WaitForCompletionAsync();
await ConsoleReporting(operation); // Change from ConsoleReporting(operation); to await ConsoleReporting(operation);
}
/// <summary>
/// Outputs the status and results of the document translation operation to the console.
/// </summary>
private static async Task ConsoleReporting(DocumentTranslationOperation operation)
{
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}");
await foreach (DocumentStatusResult document in operation.Value)
{
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: {document.TranslatedToLanguageCode}.");
Console.WriteLine($" Document source Uri: {document.SourceDocumentUri}");
}
else
{
Console.WriteLine($" Error Code: {document.Error.Code}");
Console.WriteLine($" Message: {document.Error.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment