翻译文档

Cloud Translation 高级版提供了一个 Document Translation API,用于直接翻译 PDF 和 DOCX 等格式的文档。与普通文本翻译相比,文档翻译会保留已翻译文档中的原始格式和布局,可帮助您保留很多段落内容,例如段落中断。

以下部分介绍了如何翻译文档,以及如何将文档翻译与其他 Cloud Translation 高级版功能(例如术语表和 AutoML Translation 模型)搭配使用。Document Translation 支持在线和批量翻译请求。

如需了解纯文本和 HTML 翻译,请参阅翻译文本

支持的文件格式

文档翻译支持以下输入文件类型及其关联的输出文件类型。

输入 MIME 类型文档 输出
DOC* application/msword DOC、DOCX
DOCX* application/vnd.openxmlformats-officedocument.wordprocessingml.document DOCX
PDF application/pdf PDF、DOCX
PPT application/vnd.ms-powerpoint PPT、PPTX
PPTX application/vnd.openxmlformats-officedocument.presentationml.presentation PPTX
XLS application/vnd.ms-excel XLS、XLSX
XLSX application/vnd.openxmlformats-officedocument.spreadsheetml.sheet XLSX

*文本框中的内容不会翻译并保留为源语言。

文档翻译支持原生和扫描的 PDF 文档,但存在一些差异。为实现最佳格式处理,请尽可能使用原生 PDF 文件。翻译扫描的 PDF 文件会导致一些格式丢失。复杂的 PDF 布局也可能会导致一些格式丢失,其中可能包括数据表、多列布局以及带有标签或图例的图表。

如果您有 DOCX 或 PPTX 格式的 PDF 内容,我们建议您先使用这些格式翻译内容,然后再将其转换为 PDF。通常,文档翻译保留的 DOCX 和 PPTX 文件布局和样式比 PDF 文件更好。文档翻译完成后,您可以将结果转换为 PDF 文件。

原生和扫描的 PDF 文档翻译

文档翻译支持原生和扫描的 PDF 文件,包括与从右到左的语言之间的翻译。对 PDF 到 DOCX 转换的支持仅适用于原生 PDF 文件中的批量文档翻译。此外,文档翻译仅为原生 PDF 文件保留超链接、字体大小和字体颜色(包括同步翻译和批量翻译)。

准备工作

在开始使用 Cloud Translation API 之前,您必须具有启用了 Cloud Translation API 的项目,并且必须具有适当的凭据。您还可以安装常用编程语言的客户端库,以便调用 API。如需了解详情,请参阅设置页面。

所需权限

对于需要 Cloud Storage 访问权限的请求(例如批量文档翻译请求),您可能需要 Cloud Storage 权限才能读取输入文件或将输出文件发送到存储分区。例如,要从存储分区读取输入文件,您必须至少拥有存储分区的读取对象权限(由角色 roles/storage.objectViewer 提供)。如需详细了解 Cloud Storage 角色,请参阅 Cloud Storage 文档

翻译文档(在线)

在线翻译提供单个文件的实时处理(同步处理)。

对于 PDF,文件大小不得超过 20 MB;对于原生 PDF,文件大小不得超过 300 页(要求 isTranslateNativePdfOnly 字段为 true)。如果您启用 enableShadowRemovalNativePdf 字段,则上限为 20 页。对于扫描的 PDF,上限为 20 页。

对于其他文档类型,文件大小不得超过 20 MB,无页面限制。

翻译 Cloud Storage 中的文档

以下示例演示了如何从 Cloud Storage 存储分区转换文件并将结果输出到 Cloud Storage 存储分区。该响应还会返回字节流。您可以指定 MIME 类型;如果不可用,文档翻译会使用输入文件的扩展名来确定。

如果您不指定源语言代码,Document Translation 会为您检测语言。检测到的语言包含在 detectedLanguageCode 字段的输出中。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_NUMBER_OR_ID:您的 Google Cloud 项目的数字或字母数字 ID。
  • LOCATION:您要执行此操作的区域。例如 us-central1
  • SOURCE_LANGUAGE:(可选)输入文档的语言代码。如果已知,请设置为语言支持中列出的某种语言代码。
  • TARGET_LANGUAGE:输入文档要翻译的目标语言。设置为语言支持中列出的某种语言代码。
  • INPUT_FILE_PATH:输入文档的 Cloud Storage 位置和文件名。
  • OUTPUT_FILE_PREFIX:将存储输出文档的 Cloud Storage 位置。
  • IS_NATIVE:(可选)对于原生 PDF 文档,这是一个布尔值,指示您的请求是使用原生还是扫描 PDF 页面限制。如果为 true,则仅原生 PDF 文档的页数增加至 300 页。如果为 false 或未指定,则使用扫描 PDF 页面限制(20 页)。

HTTP 方法和网址:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION:translateDocument

请求 JSON 正文:

{
  "source_language_code": "SOURCE_LANGUAGE",
  "target_language_code": "TARGET_LANGUAGE",
  "document_input_config": {
    "gcsSource": {
      "inputUri": "gs://INPUT_FILE_PATH"
    }
  },
  "document_output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://OUTPUT_FILE_PREFIX"
    }
  },
  "isTranslateNativePdfOnly": IS_NATIVE
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "documentTranslation": {
    "byteStreamOutputs": ["BYTE_STREAM"],
    "mimeType": "MIME_TYPE"
  },
  "model": "projects/PROJECT_NUMBER/locations/LOCATION/models/general/nmt"
}

Node.js

试用此示例之前,请按照 Cloud Translation 快速入门:使用客户端库中的 Node.js 设置说明进行操作。如需了解详情,请参阅 Cloud Translation Node.js API 参考文档

如需向 Cloud Translation 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const inputUri = 'path_to_your_file';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;

// Instantiates a client
const translationClient = new TranslationServiceClient();

const documentInputConfig = {
  gcsSource: {
    inputUri: inputUri,
  },
};

async function translateDocument() {
  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, location),
    documentInputConfig: documentInputConfig,
    sourceLanguageCode: 'en-US',
    targetLanguageCode: 'sr-Latn',
  };

  // Run request
  const [response] = await translationClient.translateDocument(request);

  console.log(
    `Response: Mime Type - ${response.documentTranslation.mimeType}`
  );
}

translateDocument();

翻译内嵌文档

以下示例通过请求发送内嵌文档。您必须添加内嵌文档翻译的 MIME 类型。

如果您不指定源语言代码,Document Translation 会为您检测语言。检测到的语言包含在 detectedLanguageCode 字段的输出中。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_NUMBER_OR_ID:您的 Google Cloud 项目的数字或字母数字 ID。
  • LOCATION:您要执行此操作的区域。例如 us-central1
  • SOURCE_LANGUAGE:(可选)输入文档的语言代码。如果已知,请设置为语言支持中列出的某种语言代码。
  • TARGET_LANGUAGE:输入文档要翻译的目标语言。设置为语言支持中列出的某种语言代码。
  • MIME_TYPE:源文档的格式,例如 application/pdf
  • INPUT_BYTE_STREAM:输入文档的内容,表示为字节流。
  • OUTPUT_FILE_PREFIX:将存储输出文档的 Cloud Storage 位置。
  • IS_NATIVE:(可选)对于原生 PDF 文档,这是一个布尔值,指示您的请求是使用原生还是扫描 PDF 页面限制。如果为 true,则仅原生 PDF 文档的页数增加至 300 页。如果为 false 或未指定,则使用扫描 PDF 页面限制(20 页)。

HTTP 方法和网址:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION:translateDocument

请求 JSON 正文:

{
  "source_language_code": "SOURCE_LANGUAGE",
  "target_language_code": "TARGET_LANGUAGE",
  "document_input_config": {
    "mimeType": "MIME_TYPE",
    "content": "INPUT_BYTE_STREAM"
  },
  "document_output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://OUTPUT_FILE_PREFIX"
    }
  },
  "isTranslateNativePdfOnly": IS_NATIVE
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "documentTranslation": {
    "byteStreamOutputs": ["BYTE_STREAM"],
    "mimeType": "MIME_TYPE"
  },
  "model": "projects/PROJECT_NUMBER/locations/LOCATION/models/general/nmt"
}

Python

试用此示例之前,请按照 Cloud Translation 快速入门:使用客户端库中的 Python 设置说明进行操作。如需了解详情,请参阅 Cloud Translation Python API 参考文档

如需向 Cloud Translation 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import translate_v3beta1 as translate

def translate_document(
    project_id: str,
    file_path: str,
) -> translate.TranslationServiceClient:
    """Translates a document.

    Args:
        project_id: The GCP project ID.
        file_path: The path to the file to be translated.

    Returns:
        The translated document.
    """

    client = translate.TranslationServiceClient()
    location = "us-central1"
    parent = f"projects/{project_id}/locations/{location}"

    # Supported file types: https://cloud.google.com/translate/docs/supported-formats
    with open(file_path, "rb") as document:
        document_content = document.read()

    document_input_config = {
        "content": document_content,
        "mime_type": "application/pdf",
    }

    response = client.translate_document(
        request={
            "parent": parent,
            "target_language_code": "fr-FR",
            "document_input_config": document_input_config,
        }
    )

    # To output the translated document, uncomment the code below.
    # f = open('/tmp/output', 'wb')
    # f.write(response.document_translation.byte_stream_outputs[0])
    # f.close()

    # If not provided in the TranslationRequest, the translated file will only be returned through a byte-stream
    # and its output mime type will be the same as the input file's mime type
    print(
        f"Response: Detected Language Code - {response.document_translation.detected_language_code}"
    )

    return response

使用 AutoML 模型或术语表

您可以使用自己的 AutoML Translation 模型来翻译文档,而不是由 Google 管理的模型。除了指定模型外,您还可以添加术语表,用于处理特定领域的术语。如果指定了模型或术语库,则必须指定源语言。以下示例使用 AutoML 模型和术语表。如果模型或术语表位于不同的项目中,则您必须具备访问这些资源的相应 IAM 权限。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_NUMBER_OR_ID:您的 Google Cloud 项目的数字或字母数字 ID。
  • LOCATION:您要运行此操作的区域,例如 us-central1。该位置必须与您的模型和/或术语库所在的区域匹配。
  • SOURCE_LANGUAGE:输入文档的语言代码。设置为语言支持中列出的某种语言代码。
  • TARGET_LANGUAGE:输入文档要翻译的目标语言。设置为语言支持中列出的某种语言代码。
  • INPUT_FILE_PATH:输入文档的 Cloud Storage 位置和文件名。
  • OUTPUT_FILE_PREFIX:将存储输出文档的 Cloud Storage 位置。
  • MODEL_PROJECT_ID:模型所在项目的 ID。
  • MODEL_LOCATION:模型所在的区域。
  • MODEL_ID:要使用模型的 ID。
  • GLOSSARY_PROJECT_ID:术语表所在项目的 ID。
  • GLOSSARY_LOCATION:术语表所在的区域。
  • GLOSSARY_ID:要使用的术语表的 ID。

HTTP 方法和网址:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION:translateDocument

请求 JSON 正文:

{
  "source_language_code": "SOURCE_LANGUAGE",
  "target_language_code": "TARGET_LANGUAGE",
  "document_input_config": {
    "gcsSource": {
      "inputUri": "gs://INPUT_FILE_PATH"
    }
  },
  "document_output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://OUTPUT_FILE_PREFIX"
    }
  },
  "model": "projects/MODEL_PROJECT_ID/locations/MODEL_LOCATION/models/MODEL_ID",
  "glossary_config": {
    "glossary": "projects/GLOSSARY_PROJECT_ID/locations/MODEL_LOCATION/glossaries/GLOSSARY_ID"
  }
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "documentTranslation": {
    "byteStreamOutputs": ["BYTE_STREAM"],
    "mimeType": "MIME_TYPE"
  },
  "glossary_document_translation": {
    "byteStreamOutputs": ["BYTE_STREAM_USING_GLOSSARY"],
    "mimeType": "MIME_TYPE"
  },
  "model": "projects/MODEL_PROJECT_ID/locations/MODEL_LOCATION/models/MODEL_ID",
  "glossaryConfig": {
    "glossary": "projects/GLOSSARY_PROJECT_ID/locations/MODEL_LOCATION/glossaries/GLOSSARY_ID"
  }
}

翻译文档(批量)

借助批量翻译,您可以在单个请求中将多个文件翻译为多种语言。对于每个请求,您可以发送最多 100 个文件,总内容大小不超过 1 GB 或 1 亿 Unicode 代码点,以最先达到的限额为准。您可以为每种语言指定特定的翻译模型。

翻译多个文档

以下示例包含多个输入配置。每个输入配置都是一个指向 Cloud Storage 存储桶中的文件的指针。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_NUMBER_OR_ID:您的 Google Cloud 项目的数字或字母数字 ID
  • LOCATION:您要执行此操作的区域。例如 us-central1
  • SOURCE_LANGUAGE:输入文档的语言代码。设置为语言支持中列出的某种语言代码。
  • TARGET_LANGUAGE:要将输入文档翻译成的指定语言或目标语言。请使用语言支持页面中列出的语言代码。
  • INPUT_FILE_PATH:一个或多个输入文档的 Cloud Storage 位置和文件名。
  • OUTPUT_FILE_PREFIX:存储所有输出文档的 Cloud Storage 位置。

HTTP 方法和网址:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION:batchTranslateDocument

请求 JSON 正文:

{
  "source_language_code": "SOURCE_LANGUAGE",
  "target_language_codes": ["TARGET_LANGUAGE", ...],
  "input_configs": [
    {
      "gcsSource": {
        "inputUri": "gs://INPUT_FILE_PATH_1"
      }
    },
    {
      "gcsSource": {
        "inputUri": "gs://INPUT_FILE_PATH_2"
      }
    },
    ...
  ],
  "output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://OUTPUT_FILE_PREFIX"
    }
  }
}

如需发送您的请求,请展开以下选项之一:

响应包含长时间运行的操作的 ID。
{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateDocumentMetadata",
    "state": "RUNNING"
  }
}

Node.js

试用此示例之前,请按照 Cloud Translation 快速入门:使用客户端库中的 Node.js 设置说明进行操作。如需了解详情,请参阅 Cloud Translation Node.js API 参考文档

如需向 Cloud Translation 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const inputUri = 'path_to_your_files';
// const outputUri = 'path_to_your_output_bucket';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;

// Instantiates a client
const translationClient = new TranslationServiceClient();

const documentInputConfig = {
  gcsSource: {
    inputUri: inputUri,
  },
};

async function batchTranslateDocument() {
  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, location),
    documentInputConfig: documentInputConfig,
    sourceLanguageCode: 'en-US',
    targetLanguageCodes: ['sr-Latn'],
    inputConfigs: [
      {
        gcsSource: {
          inputUri: inputUri,
        },
      },
    ],
    outputConfig: {
      gcsDestination: {
        outputUriPrefix: outputUri,
      },
    },
  };

  // Batch translate documents using a long-running operation.
  // You can wait for now, or get results later.
  const [operation] = await translationClient.batchTranslateDocument(request);

  // Wait for operation to complete.
  const [response] = await operation.promise();

  console.log(`Total Pages: ${response.totalPages}`);
}

batchTranslateDocument();

Python

试用此示例之前,请按照 Cloud Translation 快速入门:使用客户端库中的 Python 设置说明进行操作。如需了解详情,请参阅 Cloud Translation Python API 参考文档

如需向 Cloud Translation 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证


from google.cloud import translate_v3beta1 as translate

def batch_translate_document(
    input_uri: str,
    output_uri: str,
    project_id: str,
    timeout: int = 180,
) -> translate.BatchTranslateDocumentResponse:
    """Batch translate documents.

    Args:
        input_uri: Google Cloud Storage location of the input document.
        output_uri: Google Cloud Storage location of the output document.
        project_id: The GCP project ID.
        timeout: The timeout for this request.

    Returns:
        Translated document response
    """
    client = translate.TranslationServiceClient()

    # The ``global`` location is not supported for batch translation
    location = "us-central1"

    # Google Cloud Storage location for the source input. This can be a single file
    # (for example, ``gs://translation-test/input.docx``) or a wildcard
    # (for example, ``gs://translation-test/*``).
    # Supported file types: https://cloud.google.com/translate/docs/supported-formats
    gcs_source = {"input_uri": input_uri}

    batch_document_input_configs = {
        "gcs_source": gcs_source,
    }
    gcs_destination = {"output_uri_prefix": output_uri}
    batch_document_output_config = {"gcs_destination": gcs_destination}
    parent = f"projects/{project_id}/locations/{location}"

    # Supported language codes: https://cloud.google.com/translate/docs/language
    operation = client.batch_translate_document(
        request={
            "parent": parent,
            "source_language_code": "en-US",
            "target_language_codes": ["fr-FR"],
            "input_configs": [batch_document_input_configs],
            "output_config": batch_document_output_config,
        }
    )

    print("Waiting for operation to complete...")
    response = operation.result(timeout)

    print(f"Total Pages: {response.total_pages}")

    return response

翻译和转换原生 PDF 文件

以下示例翻译原生 PDF 文件并将其转换为 DOCX 文件。您可以指定各种文件类型的多个输入;它们不必都是原生 PDF 文件。但是,包含转换时不能包含扫描的 PDF 文件,否则系统会拒绝请求并且不会执行任何翻译操作。只有原生 PDF 文件才会由系统翻译并转换为 DOCX 文件。例如,如果您包含 PPTX 文件,则这些文件会由系统翻译并以 PPTX 文件的形式返回。

如果您定期翻译原生和扫描的 PDF 混合文件,我们建议您将其整理到单独的 Cloud Storage 存储桶中。这样,在请求批量翻译和转换时,您可以轻松排除包含扫描的 PDF 文件的存储桶,而不必排除个别文件。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_NUMBER_OR_ID:您的 Google Cloud 项目的数字或字母数字 ID
  • LOCATION:您要执行此操作的区域。例如 us-central1
  • SOURCE_LANGUAGE:输入文档的语言代码。设置为语言支持中列出的某种语言代码。
  • TARGET_LANGUAGE:要将输入文档翻译成的指定语言或目标语言。请使用语言支持页面中列出的语言代码。
  • INPUT_FILE_PATH:一个或多个原生 PDF 文件的 Cloud Storage 位置和文件名。
  • OUTPUT_FILE_PREFIX:存储所有输出文档的 Cloud Storage 位置。

HTTP 方法和网址:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION:batchTranslateDocument

请求 JSON 正文:

{
  "source_language_code": "SOURCE_LANGUAGE",
  "target_language_codes": ["TARGET_LANGUAGE", ...],
  "input_configs": [
    {
      "gcsSource": {
        "inputUri": "gs://INPUT_FILE_PATH_1"
      }
    },
    {
      "gcsSource": {
        "inputUri": "gs://INPUT_FILE_PATH_2"
      }
    },
    ...
  ],
  "output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://OUTPUT_FILE_PREFIX"
    }
  },
  "format_conversions": {
    "application/pdf": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  }
}

如需发送您的请求,请展开以下选项之一:

响应包含长时间运行的操作的 ID。
{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateDocumentMetadata",
    "state": "RUNNING"
  }
}

使用 AutoML 模型或术语表

您可以使用自己的 AutoML Translation 模型来翻译文档,而不是由 Google 管理的模型。除了指定模型外,您还可以添加术语表,用于处理特定领域的术语。如果指定了模型或术语库,则必须指定源语言。以下示例使用 AutoML 模型和术语表。您最多可以指定 10 种目标语言及其模型和术语表。

如果您为某些目标语言指定模型,而不为其他目标语言指定模型,则文档翻译会使用 Google 管理的模型以未指定的语言。同样,如果您为某些目标语言指定了术语表,则文档翻译功能不会对未指定的语言使用任何术语表。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_NUMBER_OR_ID:您的 Google Cloud 项目的数字或字母数字 ID
  • LOCATION:您要运行此操作的区域,例如 us-central1。该位置必须与您的模型和/或术语库所在的区域匹配。
  • SOURCE_LANGUAGE:输入文档的语言代码。设置为语言支持中列出的某种语言代码。
  • TARGET_LANGUAGE:要将输入文档翻译成的指定语言或目标语言。请使用语言支持页面中列出的语言代码。
  • INPUT_FILE_PATH:一个或多个输入文档的 Cloud Storage 位置和文件名。
  • OUTPUT_FILE_PREFIX:存储所有输出文档的 Cloud Storage 位置。
  • MODEL_PROJECT_ID:模型所在项目的 ID。
  • MODEL_LOCATION:模型所在的区域。
  • MODEL_ID:要使用模型的 ID。
  • GLOSSARY_PROJECT_ID:术语表所在项目的 ID。
  • GLOSSARY_LOCATION:术语表所在的区域。
  • GLOSSARY_ID:要使用的术语表的 ID。

HTTP 方法和网址:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION:translateDocument

请求 JSON 正文:

{
  "source_language_code": "SOURCE_LANGUAGE",
  "target_language_codes": "[TARGET_LANGUAGE, ...]",
  "input_configs": [
    {
      "gcsSource": {
        "inputUri": "gs://INPUT_FILE_PATH"
      }
    }
  ],
  "output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://OUTPUT_FILE_PREFIX"
    }
  },
  "models": {
    "TARGET_LANGUAGE": "projects/MODEL_PROJECT_ID/locations/MODEL_LOCATION/models/MODEL_ID",
    ...
  },
  "glossaries": {
    "TARGET_LANGUAGE": {
      "glossary": "projects/GLOSSARY_PROJECT_ID/locations/MODEL_LOCATION/glossaries/GLOSSARY_ID"
    },
    ...
  }
}

如需发送您的请求,请展开以下选项之一:

响应包含长时间运行的操作的 ID。
{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateDocumentMetadata",
    "state": "RUNNING"
  }
}

重叠文本

在某些情况下,原生 PDF 翻译可能包含叠加在源文本上的译文。此问题称为阴影文本。

在某些情况下,您可以通过启用 enableShadowRemovalNativePdf 选项来移除阴影文本。如果启用此选项,响应延迟时间会增加,并且对于在线文档翻译,页面数限制会减少到 20。您只能知道文档是否仅在翻译后需要移除阴影文本。

在其他情况下,如果 enableShadowRemovalNativePdf 选项不起作用,请将 PDF 转换为图片,然后进行翻译。通常,这些情况包括多层文本,例如,可选文本在也包含文本的背景图片上层。将 PDF 转换为图片使 Cloud Translation 可以将文档作为扫描的 PDF 处理。若要进行转换,您可以使用 Chrome(打印为图片)或其他第三方工具。

文本方向

对于扫描的 PDF 翻译,源文本必须为水平方向。例如,如果扫描的文档包含向上倾斜或向下倾斜的文本,则 Cloud Translation 可能无法正确解析所有文本,从而导致翻译不正确或不完整。

如果文档方向不一致,您可以让 Cloud Translation 为您确定方向。在翻译请求中,启用 enableRotationCorrection 选项以使文本在翻译之前正确确定方向。

后续步骤

  • 文档翻译按页计费。如需了解详情,请参阅价格