/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.translate;

// [START translate_v3_translate_text]
import com.google.cloud.translate.v3.LocationName;
import com.google.cloud.translate.v3.TranslateTextRequest;
import com.google.cloud.translate.v3.TranslateTextResponse;
import com.google.cloud.translate.v3.Translation;
import com.google.cloud.translate.v3.TranslationServiceClient;
import java.io.IOException;

public class TranslateText {

  public static void translateText() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR-PROJECT-ID";
    // Supported Languages: https://cloud.google.com/translate/docs/languages
    String targetLanguage = "your-target-language";
    String text = "your-text";
    translateText(projectId, targetLanguage, text);
  }

  // Translating Text
  public static void translateText(String projectId, String targetLanguage, String text)
      throws IOException {

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
      // Supported Locations: `global`, [glossary location], or [model location]
      // Glossaries must be hosted in `us-central1`
      // Custom Models must use the same location as your model. (us-central1)
      LocationName parent = LocationName.of(projectId, "global");

      // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
      TranslateTextRequest request =
          TranslateTextRequest.newBuilder()
              .setParent(parent.toString())
              .setMimeType("text/plain")
              .setTargetLanguageCode(targetLanguage)
              .addContents(text)
              .build();

      TranslateTextResponse response = client.translateText(request);

      // Display the translation for each input text provided
      for (Translation translation : response.getTranslationsList()) {
        System.out.printf("Translated text: %s\n", translation.getTranslatedText());
      }
    }
  }
}
// [END translate_v3_translate_text]

@@ -655,16 +655,106 @@
 e_text%5D%0A
+// %5BSTART translate_v3_translate_text_0%5D%0A// Imports the Google Cloud Translation library.%0A
 import c
@@ -1061,37 +1061,202 @@
 n;%0A%0A
-public class TranslateText %7B%0A
+// %5BEND translate_v3_translate_text_0%5D%0A%0Apublic class TranslateText %7B%0A%0A  // %5BSTART translate_v3_translate_text_1%5D%0A  // Set and pass variables to overloaded translateText() method for translation.
 %0A  p
@@ -1648,30 +1648,132 @@
   %7D%0A
-%0A  // Translating Text
+  // %5BEND translate_v3_translate_text_1%5D%0A%0A  // %5BSTART translate_v3_translate_text_2%5D%0A  // Translate text to target language.
 %0A  p
@@ -3209,16 +3209,57 @@
   %7D%0A  %7D%0A
+  // %5BEND translate_v3_translate_text_2%5D%0A
 %7D%0A// %5BEN

Add Cloud Code tags for API Explorer pilot (#840)