文档翻译客户端库 SDK

文档翻译Azure 翻译器服务的一个基于云的功能。 可以采用各种文件格式翻译整个文档或处理批量文档翻译,同时保留原始文档结构和格式。 在本文中,你会了解如何使用文档翻译服务 C#/.NET 和 Python 客户端库。 对于 REST API,请参阅我们的快速入门指南。

先决条件

要开始,需要:

  • 一个有效的 Azure 帐户。 如果没有,可以创建一个试用帐户

  • 单服务的 Translator 资源(并非多服务的认知服务资源) 。 选择区域。 选择“标准 S1”定价层以开始使用(免费层不支持文档翻译)。

  • 一个 Azure Blob 存储帐户。 你将在自己的 Azure Blob 存储帐户中为源和目标文件创建容器

    • 源容器。 将在此容器中上传要翻译的文件(必需)。
    • 目标容器。 在此容器中存储已翻译的文件(必需)。
  • 还需要为源容器和目标容器创建共享访问签名 (SAS) 令牌。 sourceUrltargetUrl 必须包含作为查询字符串追加的共享访问签名 (SAS) 令牌。 可将该令牌分配到容器或特定的 Blob。 参阅为文档翻译处理创建 SAS 令牌

    • 容器或 Blob 必须已指定读取列出访问权限。
    • 目标容器或 Blob 必须已指定写入列出访问权限。

有关详细信息,请参阅创建 SAS 令牌

客户端库

| 包 (NuGet)| 客户端库 | REST API | 产品文档 | 示例 |

设置项目

在控制台窗口(例如 cmd、PowerShell 或 Bash)中,使用 dotnet new 命令创建名为 batch-document-translation 的新控制台应用。 此命令将创建包含单个源文件的简单“Hello World”C# 项目:program.cs

dotnet new console -n batch-document-translation

将目录更改为新创建的应用文件夹。 使用以下命令生成应用程序:

dotnet build

生成输出不应包含警告或错误。

...
Build succeeded.
 0 Warning(s)
 0 Error(s)
...

安装客户端库

在应用程序目录中,使用以下方法之一安装适用于 .NET 的文档翻译客户端库:

.NET CLI

dotnet add package Azure.AI.Translation.Document --version 1.0.0

NuGet 程序包管理器

Install-Package Azure.AI.Translation.Document -Version 1.0.0

NuGet PackageReference

<ItemGroup>
    <!-- ... -->
<PackageReference Include="Azure.AI.Translation.Document" Version="1.0.0" />
    <!-- ... -->
</ItemGroup>

在首选的编辑器或 IDE 中,从项目目录打开 Program.cs 文件。 添加以下 using 指令:

using Azure;
using Azure.AI.Translation.Document;

using System;
using System.Threading;

在应用程序的“Program”类中,为密钥和自定义终结点创建变量。 有关详细信息,请参阅检索密钥和自定义域终结点

private static readonly string endpoint = "<your custom endpoint>";
private static readonly string key = "<your key>";

翻译文档或批处理文件

  • 若要针对单个 Blob 容器中的一个或多个文档启动翻译操作,请调用 StartTranslationAsync 方法。

  • 若要调用 StartTranslationAsync,需要初始化包含以下参数的 DocumentTranslationInput 对象:

  • sourceUri。 包含要翻译的文档的源容器的 SAS URI。

  • targetUri 翻译后的文档要写入到的目标容器的 SAS URI。

  • targetLanguageCode。 翻译后的文档的语言代码。 可以在我们的语言支持页上找到语言代码。


public void StartTranslation() {
  Uri sourceUri = new Uri("<sourceUrl>");
  Uri targetUri = new Uri("<targetUrl>");

  DocumentTranslationClient client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(key));

  DocumentTranslationInput input = new DocumentTranslationInput(sourceUri, targetUri, "es")

  DocumentTranslationOperation operation = await client.StartTranslationAsync(input);

  await operation.WaitForCompletionAsync();

  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.DocumentId}");
    Console.WriteLine($ "  Status:{document.Status}");
    if (document.Status == TranslationStatus.Succeeded) {
      Console.WriteLine($ "  Translated Document Uri: {document.TranslatedDocumentUri}");
      Console.WriteLine($ "  Translated to language: {document.TranslatedTo}.");
      Console.WriteLine($ "  Document source Uri: {document.SourceDocumentUri}");
    }
    else {
      Console.WriteLine($ "  Error Code: {document.Error.ErrorCode}");
      Console.WriteLine($ "  Message: {document.Error.Message}");
    }
  }
}

就这么简单! 现已创建一个可以使用 .NET 客户端库翻译存储容器中的文档的程序。

后续步骤