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 you must have 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 folder 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
This example shows two input files sent for translating.Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
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, expand one of these options:
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" } }
Go
Before trying this sample, follow the Go setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Go API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Java API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for Ruby.
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
This example shows how to specify a custom model for the target language.Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
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, expand one of these options:
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" } }
Go
Before trying this sample, follow the Go setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Go API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Java API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for Ruby.
Specifying AutoML models for multiple target languages
REST
When you have multiple target languages, you can specify a custom model for each target language.Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
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, expand one of these options:
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
This example shows how to specify a glossary for the target language.Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
- 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 named request.json
,
and execute the following command:
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
Save the request body in a file named request.json
,
and execute the following command:
$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
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" } }
Go
Before trying this sample, follow the Go setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Go API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Java API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for Ruby.
Translating text using an AutoML Translation custom model and glossary
REST
This example shows how to specify a custom model and glossary for the target language.Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
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 named request.json
,
and execute the following command:
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
Save the request body in a file named request.json
,
and execute the following command:
$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
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" } }
Go
Before trying this sample, follow the Go setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Go API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Java API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the Cloud Translation reference documentation for Ruby.
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.
Additional resources
- For help on resolving common issues or errors, see the Troubleshooting page.