Batch requests(v3beta1)

Batch translation allows you to translate large amounts of text (with a limit of 1,000 files in a 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.

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. We recommend each row be <= 10K Unicode codepoints, otherwise an error maybe returned.

The other supported file extensions are .txt or .html, which is treated as a single large chunk of text.

With a batch translation request, you provide the path to an input configuration file (InputConfig) containing the content you want translated and to an output location (OutputConfig) for the final translation.

As the request is processing, we write the results to the output location in real time. Even if a user cancels the request half way through, input file-level partial output are still produced in the output gcs location. Therefore, the translated number of characters are still charged. See reference documentation for the detailed execution logic and output format.

curl command

curl -X POST \
     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
         source_language_code: 'en',
         target_language_codes: ['zh', 'ja'],
         input_configs: {
              gcs_source: {
                   input_uri: 'gs://bucket-name/input-file-name',
             },
         },
         output_config: {
             gcs_destination: {
                output_uri_prefix: 'gs://bucket-name/',
             },
         }
      }" "https://translation.googleapis.com/v3beta1/projects/project-id/locations/us-central1:batchTranslateText"

You can check the status of a batch translation using the operation ID from the response. In the command below, replace operation-id with the operation ID returned by the request.

curl -X GET \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  https://translation.googleapis.com/v3beta1/projects/project-id/locations/us-central1/operations/operation-id

To cancel a batch translation request:

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  https://translation.googleapis.com/v3beta1/projects/project-id/locations/us-central1/operations/operation-id:cancel

Java

Before trying this sample, follow the Java setup instructions in the Translation API Quickstart Using Client Libraries . For more information, see the Translation API Java API reference documentation .

static BatchTranslateResponse batchTranslateText(
    String projectId, String location, String sourceUri, String destinationUri) {
  try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {

    LocationName locationName =
        LocationName.newBuilder().setProject(projectId).setLocation(location).build();
    GcsSource gcsSource = GcsSource.newBuilder().setInputUri(sourceUri).build();
    InputConfig inputConfig =
        InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
    GcsDestination gcsDestination =
        GcsDestination.newBuilder().setOutputUriPrefix(destinationUri).build();
    OutputConfig outputConfig =
        OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
    BatchTranslateTextRequest batchTranslateTextRequest =
        BatchTranslateTextRequest.newBuilder()
            .setParent(locationName.toString())
            .setSourceLanguageCode("en")
            .addTargetLanguageCodes("sr")
            .addInputConfigs(inputConfig)
            .setOutputConfig(outputConfig)
            .build();

    // Call the API
    BatchTranslateResponse response =
        translationServiceClient
            .batchTranslateTextAsync(batchTranslateTextRequest)
            .get(300, TimeUnit.SECONDS);

    System.out.printf("Total Characters: %d\n", response.getTotalCharacters());
    System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters());
    return response;

  } catch (Exception e) {
    throw new RuntimeException("Couldn't create client.", e);
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Translation API Quickstart Using Client Libraries . For more information, see the Translation API Node.js API reference documentation .

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

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

// Instantiates a client
const translationClient = new TranslationServiceClient();
async function batchTranslateText() {
  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, location),
    sourceLanguageCode: 'en-US',
    targetLanguageCodes: ['sr-Latn'],
    inputConfigs: [
      {
        mimeType: 'text/plain', // mime types: text/plain, text/html
        gcsSource: {
          inputUri: inputUri,
        },
      },
    ],
    outputConfig: {
      gcsDestination: {
        outputUriPrefix: outputUri,
      },
    },
  };

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

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

  console.log(`Total Characters: ${response.totalCharacters}`);
  console.log(`Translated Characters: ${response.translatedCharacters}`);
}

batchTranslateText();

Python

Before trying this sample, follow the Python setup instructions in the Translation API Quickstart Using Client Libraries . For more information, see the Translation API Python API reference documentation .

from google.cloud import translate_v3beta1 as translate
client = translate.TranslationServiceClient()

# project_id = YOUR_PROJECT_ID
# input_uri = 'gs://cloud-samples-data/translation/text.txt'
# output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'
location = 'us-central1'

parent = client.location_path(project_id, location)

gcs_source = translate.types.GcsSource(input_uri=input_uri)

input_config = translate.types.InputConfig(
    mime_type='text/plain',  # mime types: text/plain, text/html
    gcs_source=gcs_source)

gcs_destination = translate.types.GcsDestination(
    output_uri_prefix=output_uri)

output_config = translate.types.OutputConfig(
    gcs_destination=gcs_destination)

operation = client.batch_translate_text(
    parent=parent,
    source_language_code='en-US',
    target_language_codes=['sr-Latn'],
    input_configs=[input_config],
    output_config=output_config)

result = operation.result(90)

print('Total Characters: {}'.format(result.total_characters))
print('Translated Characters: {}'.format(result.translated_characters))

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Translation
Need help? Visit our support page.