Batch translation allows you to translate large amounts of text (with a limit of 100 files per batch), and to up to 10 different target languages in a command offline. The total content size should be <= 100M Unicode codepoints and must use UTF-8 encoding.
Before you begin
Before you can start using the Cloud Translation API, you must have a project that has the Cloud Translation API enabled and the appropriate credentials. You can also install client libraries for common programming languages to help you make calls to the API.
For more information, see the Setup page.
Permissions
For batch translations, you must have access to Cloud Storage buckets in
addition to the Cloud Translation permissions.
Batch translation input files are read from a Cloud Storage bucket and output
files are written to a Cloud Storage bucket. For example, to read input files
from a bucket, you must have at least read object permissions (provided by the
role roles/storage.objectViewer
) on the bucket. For more information about
Cloud Storage roles, see the Cloud Storage
documentation.
Input file
Only two MIME types are supported: text/html (HTML) and text/plain (.tsv and .txt).
Using a TSV file
If a file extension is TSV, it can contain either one or two columns. The first column (optional) is the ID of the text request. If the first column is missing, Google uses the row number (0-based) from the input file as the ID in the output file. The second column is the actual text to be translated. For best results, each row be less than or equal to 10K Unicode codepoints, otherwise an error may be returned.
Using text or HTML
The other supported file extensions are text files (.txt) or HTML, which is treated as a single large chunk of text.
Batch request
With a batch translation request, you provide the path to an input configuration file (InputConfig) containing the content you want translated and provide a path to an output location (OutputConfig) for the final translation. You need at least two different Cloud Storage buckets. The source bucket contains content to be translated, and the destination bucket will contain the resulting translated file(s). The destination bucket must be empty before the translation process begins.
As the request is processing, we write the results to the output location in real time. Even if you cancel the request halfway through, input file-level partial output are still produced in the output Cloud Storage location. Therefore, the translated number of characters are still charged.
REST & CMD LINE
This example shows two input files sent for translating.Before using any of the request data below, make the following replacements:
- project-number-or-id: your Google Cloud project number or ID
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:batchTranslateText
Request JSON body:
{ "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/" } } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-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
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
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
You should receive a JSON response similar to the following:
{ "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" } }
C#
Before trying this sample, follow the C# setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Node.js API reference documentation.
PHP
Before trying this sample, follow the PHP setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation PHP API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Ruby API reference documentation.
Making a batch request using an AutoML model
You can use a custom model for batch requests. There are various scenarios when multiple target languages are involved.
Specifying an AutoML model for target language
REST & CMD LINE
This example shows how to specify a custom model for the target language.Before using any of the request data below, make the following replacements:
- project-number-or-id: your Google Cloud project number or ID
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:batchTranslateText
Request JSON body:
{ "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/" } } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-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
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
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
You should receive a JSON response similar to the following:
{ "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" } }
C#
Before trying this sample, follow the C# setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Node.js API reference documentation.
PHP
Before trying this sample, follow the PHP setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation PHP API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Ruby API reference documentation.
Specifying AutoML models for multiple target languages
REST & CMD LINE
When you have multiple target languages, you can specify a custom model for each target language.Before using any of the request data below, make the following replacements:
- project-number-or-id: your Google Cloud project number or ID
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:batchTranslateText
Request JSON body:
{ "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/" } } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-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
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
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
You should receive a JSON response similar to the following:
{ "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" } }
Specifying an AutoML model for a target language and not others
You can specify a custom model for a particular target language while not
specifying a model for the other target languages. Using the code for
Specifying custom models for multiple target language, just modify the
models
field to specify the target language for the model, es
in
this example, and leaving fr
unspecified:
"models": {'es':'projects/project-number-or-id/locations/us-central1/models/model-id'},
where project-number-or-id
is your Google Cloud project number
or ID, and model-id
is the name you've given your AutoML model.
Translating text using a glossary
REST & CMD LINE
This example shows how to specify a glossary for the target language.Before using any of the request data below, make the following replacements:
- project-number-or-id: your Google Cloud project number or ID
- glossary-id: your glossary ID, for example, "my-en-to-es-glossary"
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:batchTranslateText
Request JSON body:
{ "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/" } } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-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
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
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
You should receive a JSON response similar to the following:
{ "name": "projects/project-number/locations/us-central1/operations/operation-id", "metadata": { "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateMetadata", "state": "RUNNING" } }
C#
Before trying this sample, follow the C# setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Node.js API reference documentation.
PHP
Before trying this sample, follow the PHP setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation PHP API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Ruby API reference documentation.
Translating text using an AutoML Translation custom model and glossary
REST & CMD LINE
This example shows how to specify a custom model and glossary for the target language.Before using any of the request data below, make the following replacements:
- project-number-or-id: your Google Cloud project number or ID
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:batchTranslateText
Request JSON body:
{ "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/" } } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-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
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
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
You should receive a JSON response similar to the following:
{ "name": "projects/project-number/locations/us-central1/operations/operation-id", "metadata": { "@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateMetadata", "state": "RUNNING" } }
C#
Before trying this sample, follow the C# setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Node.js API reference documentation.
PHP
Before trying this sample, follow the PHP setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation PHP API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Ruby API reference documentation.
Operation status
A batch request is a long-running operation, so it may take a substantial amount of time to complete. You can poll the status of this operation to see if it has completed, or you can cancel the operation.
For more information, see Long-running operations.