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/',
      }" ""

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" \

To cancel a batch translation request:

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \


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 .

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

    LocationName locationName =
    GcsSource gcsSource = GcsSource.newBuilder().setInputUri(sourceUri).build();
    InputConfig inputConfig =
    GcsDestination gcsDestination =
    OutputConfig outputConfig =
    BatchTranslateTextRequest batchTranslateTextRequest =

    // Call the API
    BatchTranslateResponse response =
            .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);


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 .

 * 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}`);



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 .

from 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_destination = translate.types.GcsDestination(

output_config = translate.types.OutputConfig(

operation = client.batch_translate_text(

result = operation.result(90)

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

Var denne side nyttig? Giv os en anmeldelse af den:

Send feedback om...

Cloud Translation
Har du brug for hjælp? Besøg vores supportside.