批次翻譯可讓您翻譯大量文字 (每個批次的限制為 100 個檔案),並且以離線指令翻譯為最多 10 種不同的目標語言。總體內容大小不得超過 100M Unicode 碼點,且必須使用 UTF-8 編碼。
事前準備
如要開始使用 Cloud Translation API,您必須擁有已啟用 Cloud Translation API 的專案,並具備適當的憑證。您也可以安裝常用程式設計語言的用戶端程式庫,協助您呼叫 API。詳情請參閱「設定」頁面。
權限
如要進行批次翻譯,除了 Cloud Translation 權限,您也必須有權存取 Cloud Storage 值區。批次翻譯輸入檔案會從 Cloud Storage 值區讀取,輸出檔案則會寫入 Cloud Storage 值區。舉例來說,如要從值區讀取輸入檔案,您必須至少具備該值區的讀取物件權限 (由 roles/storage.objectViewer
角色提供)。如要進一步瞭解 Cloud Storage 角色,請參閱 Cloud Storage 說明文件。
輸入檔案
僅支援以下兩種 MIME 類型:text/html (HTML) 和 text/plain (.tsv 和 .txt)。
使用 TSV 檔案
如果副檔名為 TSV,則檔案可以包含一或二個資料欄。第一個資料欄 (選用) 是文字要求的 ID。如果第一個資料欄遺失,Google 會使用輸入檔案中的資料列編號 (從 0 開始) 作為輸出檔案中的 ID。第二個資料欄是要翻譯的實際文字。為獲得最佳結果,每個資料列應小於或等於 10K Unicode 碼點,否則可能會傳回錯誤。
使用文字或 HTML
其他支援的副檔名為文字檔案 (.txt) 或 HTML,系統會將其視為一個大型文字區塊。
批次要求
發出批次翻譯要求時,您必須提供以下兩者的路徑:含有待翻譯內容的輸入設定檔 (InputConfig) 及最終翻譯的輸出位置 (OutputConfig)。您至少需要兩個不同的 Cloud Storage 值區。來源值區包含待翻譯的內容,目標值區則包含翻譯後產生的檔案。在翻譯程序開始之前,目標資料夾必須是空的。
處理要求時,我們會即時將結果寫入輸出位置。即使您在中途取消要求,系統仍會在 Cloud Storage 輸出位置產生輸入檔案層級的部分輸出內容。因此,您仍須支付已翻譯字元數的費用。
REST
以下範例顯示兩個送交翻譯的輸入檔案。使用任何要求資料之前,請先替換以下項目:
- PROJECT_NUMBER_OR_ID: Google Cloud 專案的數值或英數字元 ID
HTTP 方法和網址:
POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText
JSON 要求主體:
{ "sourceLanguageCode": "en", "targetLanguageCodes": ["es", "fr"], "inputConfigs": [ { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name1" } }, { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name2" } } ], "outputConfig": { "gcsDestination": { "outputUriPrefix": "gs://bucket-name-destination/" } } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "name": "projects/project-number/locations/us-central1/operations/20191107-08251564068323-5d3895ce-0000-2067-864c-001a1136fb06", "metadata": { "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateMetadata", "state": "RUNNING" } }
Go
在試用這個範例之前,請先按照Go使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Go API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
在試用這個範例之前,請先按照Java使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Java API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Node.js
在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Node.js API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
在試用這個範例之前,請先按照Python使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Python API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
其他語言
C#: 請按照用戶端程式庫頁面的 C# 設定說明操作, 然後前往 .NET 適用的 Cloud Translation 參考說明文件。
PHP: 請按照用戶端程式庫頁面的 PHP 設定說明 操作,然後前往 PHP 適用的 Cloud Translation 參考說明文件。
Ruby: 請按照用戶端程式庫頁面的 Ruby 設定說明 操作,然後前往 Ruby 適用的 Cloud Translation 參考文件。
使用 AutoML 模型發出批次要求
您可以在批次要求中使用自訂模型。如果涉及多種目標語言,可能會發生各種情況。
指定目標語言的 AutoML 模型
REST
這個範例說明如何為目標語言指定自訂模型。使用任何要求資料之前,請先替換以下項目:
- PROJECT_NUMBER_OR_ID: Google Cloud 專案的數值或英數字元 ID
HTTP 方法和網址:
POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText
JSON 要求主體:
{ "models":{"es":"projects/PROJECT_NUMBER_OR_ID/locations/us-central1/models/model-id"}, "sourceLanguageCode": "en", "targetLanguageCodes": ["es"], "inputConfigs": [ { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name1" } }, { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name2" } } ], "outputConfig": { "gcsDestination": { "outputUriPrefix": "gs://bucket-name-destination/" } } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "name": "projects/project-number/locations/us-central1/operations/20190725-08251564068323-5d3895ce-0000-2067-864c-001a1136fb06", "metadata": { "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateMetadata", "state": "RUNNING" } }
Go
在試用這個範例之前,請先按照Go使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Go API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
在試用這個範例之前,請先按照Java使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Java API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Node.js
在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Node.js API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
在試用這個範例之前,請先按照Python使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Python API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
其他語言
C#: 請按照用戶端程式庫頁面的 C# 設定說明操作, 然後前往 .NET 適用的 Cloud Translation 參考說明文件。
PHP: 請按照用戶端程式庫頁面的 PHP 設定說明 操作,然後前往 PHP 適用的 Cloud Translation 參考說明文件。
Ruby: 請按照用戶端程式庫頁面的 Ruby 設定說明 操作,然後前往 Ruby 適用的 Cloud Translation 參考文件。
為多種目標語言指定 AutoML 模型
REST
如果有多種譯文語言,可以為每種譯文語言指定自訂模型。使用任何要求資料之前,請先替換以下項目:
- PROJECT_NUMBER_OR_ID: Google Cloud 專案的數值或英數字元 ID
HTTP 方法和網址:
POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText
JSON 要求主體:
{ "models":{ "es":"projects/PROJECT_NUMBER_OR_ID/locations/us-central1/models/model-id1", "fr":"projects/PROJECT_NUMBER_OR_ID/locations/us-central1/models/model-id2"}, "sourceLanguageCode": "en", "targetLanguageCodes": ["es", "fr"], "inputConfigs": [ { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name1" } }, { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name2" } } ], "outputConfig": { "gcsDestination": { "outputUriPrefix": "gs://bucket-name-destination/" } } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "name": "projects/project-number/locations/us-central1/operations/20191105-08251564068323-5d3895ce-0000-2067-864c-001a1136fb06", "metadata": { "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateMetadata", "state": "RUNNING" } }
為目標語言指定 AutoML 模型,但未為其他語言指定
您可以為特定目標語言指定自訂模型,但不必為其他目標語言指定模型。使用「為多種目標語言指定自訂模型」的程式碼,修改 models
欄位,指定模型的目標語言 (本例為 es
),並將 fr
留空:
"models": {'es':'projects/PROJECT_NUMBER_OR_ID/locations/us-central1/models/model-id'},
其中 PROJECT_NUMBER_OR_ID
是您的 Google Cloud 專案編號
或 ID,model-id
則是您為 AutoML 模型指定的名稱。
使用詞彙表翻譯文字
REST
這個範例說明如何指定目標語言的字彙表。使用任何要求資料之前,請先替換以下項目:
- PROJECT_NUMBER_OR_ID: Google Cloud 專案的數值或英數字元 ID
- glossary-id:您的詞彙表 ID,例如「my-en-to-es-glossary」
HTTP 方法和網址:
POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText
JSON 要求主體:
{ "sourceLanguageCode": "en", "targetLanguageCodes": ["es"], "glossaries": { "es": { "glossary": "projects/PROJECT_NUMBER_OR_ID/locations/us-central1/glossaries/glossary-id" } }, "inputConfigs": [{ "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name1" } }, { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name2" } } ], "outputConfig": { "gcsDestination": { "outputUriPrefix": "gs://bucket-name-destination/" } } }
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_NUMBER_OR_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText"
PowerShell
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER_OR_ID" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText" | Select-Object -Expand Content
您應該會收到如下的 JSON 回應:
{ "name": "projects/project-number/locations/us-central1/operations/operation-id", "metadata": { "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateMetadata", "state": "RUNNING" } }
Go
在試用這個範例之前,請先按照Go使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Go API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
在試用這個範例之前,請先按照Java使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Java API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Node.js
在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Node.js API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
在試用這個範例之前,請先按照Python使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Python API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
其他語言
C#: 請按照用戶端程式庫頁面的 C# 設定說明操作, 然後前往 .NET 適用的 Cloud Translation 參考說明文件。
PHP: 請按照用戶端程式庫頁面的 PHP 設定說明 操作,然後前往 PHP 適用的 Cloud Translation 參考說明文件。
Ruby: 請按照用戶端程式庫頁面的 Ruby 設定說明 操作,然後前往 Ruby 適用的 Cloud Translation 參考文件。
使用 AutoML Translation 自訂模型和詞彙翻譯文字
REST
這個範例說明如何為目標語言指定自訂模型和詞彙表。使用任何要求資料之前,請先替換以下項目:
- PROJECT_NUMBER_OR_ID: Google Cloud 專案的數值或英數字元 ID
HTTP 方法和網址:
POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText
JSON 要求主體:
{ "models": { "es": "projects/project_number_or_id/locations/us-central1/models/model-id" }, "sourceLanguageCode": "en", "targetLanguageCodes": ["es"], "glossaries": { "es": { "glossary": "projects/project_number_or_id/locations/us-central1/glossaries/glossary-id" } }, "inputConfigs": [{ "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name" } }, { "gcsSource": { "inputUri": "gs://bucket-name-source/input-file-name2" } } ], "outputConfig": { "gcsDestination": { "outputUriPrefix": "gs://bucket-name-destination/" } } }
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_NUMBER_OR_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText"
PowerShell
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER_OR_ID" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/us-central1:batchTranslateText" | Select-Object -Expand Content
您應該會收到如下的 JSON 回應:
{ "name": "projects/project-number/locations/us-central1/operations/operation-id", "metadata": { "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateMetadata", "state": "RUNNING" } }
Go
在試用這個範例之前,請先按照Go使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Go API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
在試用這個範例之前,請先按照Java使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Java API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Node.js
在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Node.js API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
在試用這個範例之前,請先按照Python使用用戶端程式庫的 Cloud Translation 快速入門導覽課程中的操作說明進行設定。詳情請參閱 Cloud Translation Python API 參考說明文件。
如要向 Cloud Translation 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
其他語言
C#: 請按照用戶端程式庫頁面的 C# 設定說明操作, 然後前往 .NET 適用的 Cloud Translation 參考說明文件。
PHP: 請按照用戶端程式庫頁面的 PHP 設定說明 操作,然後前往 PHP 適用的 Cloud Translation 參考說明文件。
Ruby: 請按照用戶端程式庫頁面的 Ruby 設定說明 操作,然後前往 Ruby 適用的 Cloud Translation 參考文件。
作業狀態
批次要求是一項長時間執行的作業,可能需要相當長的時間才能完成。您可以輪詢這項作業的狀態來查看作業是否已完成,也可以取消該項作業。
詳情請參閱長時間執行的作業一文。
其他資源
- 如需解決常見問題或錯誤的說明,請參閱「疑難排解」頁面。