翻譯文件

Cloud Translation - Advanced 提供 Document Translation API,可直接翻譯格式化文件,例如 PDF 和 DOCX。相較於純文字翻譯,文件翻譯可保留翻譯文件的原始格式和版面配置,協助您保留大部分的原始內容,例如段落分隔。

以下各節說明如何翻譯文件,以及如何搭配使用 Document Translation 和其他 Cloud Translation - Advanced 功能,例如詞彙和 AutoML Translation 模型。文件翻譯支援線上和批次翻譯要求。

如要翻譯純文字和 HTML,請參閱「翻譯文字」。

支援的檔案格式

文件翻譯支援下列輸入檔案類型和相關聯的輸出檔案類型。

輸入內容 文件 MIME 類型 輸出
DOC* 應用程式/msword DOC、DOCX
DOCX* application/vnd.openxmlformats-officedocument.wordprocessingml.document DOCX
PDF 應用程式/pdf PDF、DOCX
PPT 應用程式/vnd.ms-powerpoint PPT、PPTX
PPTX application/vnd.openxmlformats-officedocument.presentationml.presentation PPTX
XLS 應用程式/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 檔案的批次文件翻譯作業,才支援將 PDF 轉換為 DOCX 檔案。此外,文件翻譯功能只會保留原生 PDF 檔案的超連結、字型大小和字型顏色 (適用於同步和批次翻譯)。

如果 PDF 檔案同時包含掃描和原生 PDF 內容,系統不會翻譯掃描內容。

事前準備

如要開始使用 Cloud Translation API,您必須擁有已啟用 Cloud Translation API 的專案,並具備適當的憑證。您也可以安裝常用程式設計語言的用戶端程式庫,協助您呼叫 API。詳情請參閱「設定」頁面。

所需權限

如果要求需要存取 Cloud Storage,例如批次文件翻譯,您可能需要 Cloud Storage 權限才能讀取輸入檔案,或將輸出檔案傳送至 bucket。舉例來說,如要從值區讀取輸入檔案,您必須至少具備該值區的讀取物件權限 (由 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:專案的數值或英數字元 ID。 Google Cloud
  • LOCATION:您要執行這項作業的區域。例如:us-central1
  • SOURCE_LANGUAGE:(選用) 輸入文件的語言代碼。如果知道語言代碼,請設為「語言支援」頁面列出的其中一個代碼。
  • TARGET_LANGUAGE:輸入文件要翻譯成的目標語言。設為「語言支援」中列出的其中一個語言代碼。
  • INPUT_FILE_PATH:輸入文件的 Cloud Storage 位置和檔案名稱。
  • OUTPUT_FILE_PREFIX:輸出文件儲存的 Cloud Storage 位置。
  • IS_NATIVE:(選用) 針對原生 PDF 文件,這個布林值表示要求是否使用原生或掃描 PDF 頁面限制。如果為 true,則頁數上限會增加至 300 頁,但僅限原生 PDF 文件。如果設為 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

在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 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:專案的數值或英數字元 ID。 Google Cloud
  • LOCATION:您要執行這項作業的區域。例如:us-central1
  • SOURCE_LANGUAGE:(選用) 輸入文件的語言代碼。如果知道語言代碼,請設為「語言支援」頁面列出的其中一個代碼。
  • TARGET_LANGUAGE:輸入文件要翻譯成的目標語言。設為「語言支援」中列出的其中一個語言代碼。
  • MIME_TYPE:來源文件的格式,例如 application/pdf
  • INPUT_BYTE_STREAM:以位元組串流表示的輸入文件內容。
  • OUTPUT_FILE_PREFIX:輸出文件儲存的 Cloud Storage 位置。
  • IS_NATIVE:(選用) 針對原生 PDF 文件,這個布林值表示要求是否使用原生或掃描 PDF 頁面限制。如果為 true,則頁數上限會增加至 300 頁,但僅限原生 PDF 文件。如果設為 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

在試用這個範例之前,請先按照Python使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 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 模型或字彙表

您可以選擇不使用 Google 代管的模型,改用自己的 AutoML Translation 模型翻譯文件。除了指定模型,您也可以加入詞彙表,處理特定領域的專有術語。如果指定模型或字彙表,就必須指定原文語言。以下範例使用 AutoML 模型和詞彙表。如果模型或字彙表位於其他專案,您必須具備相應的 IAM 權限,才能存取這些資源。

REST

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_NUMBER_OR_ID:專案的數值或英數字元 ID。 Google Cloud
  • 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

在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 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

在試用這個範例之前,請先按照Python使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 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 bucket。這樣一來,當您要求批次翻譯和轉換時,就能輕鬆排除含有 PDF 掃描檔的 bucket,不必排除個別檔案。

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 模型或字彙表

您可以選擇不使用 Google 代管的模型,改用自己的 AutoML Translation 模型翻譯文件。除了指定模型,您也可以加入詞彙表,處理特定領域的專有術語。如果指定模型或字彙表,就必須指定原文語言。以下範例使用 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 選項,確保文字方向正確再進行翻譯。

後續步驟

  • 文件翻譯功能是按頁數計費。詳情請參閱「定價」一文。