use of com.google.cloud.translate.Detection in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateSnippets method testDetectLanguageOfTextList.
@Test
public void testDetectLanguageOfTextList() {
// [START translate_detect_language]
// TODO(developer): Uncomment these lines.
// import com.google.cloud.translate.*;
// Translate translate = TranslateOptions.getDefaultInstance().getService();
List<String> texts = new LinkedList<>();
texts.add("Hello, World!");
texts.add("¡Hola Mundo!");
List<Detection> detections = translate.detect(texts);
System.out.println("Language(s) detected:");
for (Detection detection : detections) {
System.out.printf("\t%s\n", detection);
}
// [END translate_detect_language]
Detection detection = detections.get(0);
assertEquals("en", detection.getLanguage());
detection = detections.get(1);
assertEquals("es", detection.getLanguage());
}
use of com.google.cloud.translate.Detection in project google-cloud-java by GoogleCloudPlatform.
the class DetectLanguageAndTranslate method main.
public static void main(String... args) {
// Create a service object
//
// If no explicit credentials or API key are set, requests are authenticated using Application
// Default Credentials if available; otherwise, using an API key from the GOOGLE_API_KEY
// environment variable
Translate translate = TranslateOptions.getDefaultInstance().getService();
// Text of an "unknown" language to detect and then translate into English
final String mysteriousText = "Hola Mundo";
// Detect the language of the mysterious text
Detection detection = translate.detect(mysteriousText);
String detectedLanguage = detection.getLanguage();
// Translate the mysterious text to English
Translation translation = translate.translate(mysteriousText, TranslateOption.sourceLanguage(detectedLanguage), TranslateOption.targetLanguage("en"));
System.out.println(translation.getTranslatedText());
}
use of com.google.cloud.translate.Detection in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateSnippets method testDetectLanguageOfText.
@Test
public void testDetectLanguageOfText() {
// SNIPPET translate_detect_language_string
Detection detection = translate.detect("Hello, World!");
// SNIPPET translate_detect_language_string
assertEquals("en", detection.getLanguage());
}
Aggregations