use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateSnippets method testTranslateText.
@Test
public void testTranslateText() {
// [START translate_translate_text]
// TODO(developer): Uncomment these lines.
// import com.google.cloud.translate.*;
// Translate translate = TranslateOptions.getDefaultInstance().getService();
Translation translation = translate.translate("¡Hola Mundo!");
System.out.printf("Translated Text:\n\t%s\n", translation.getTranslatedText());
// [END translate_translate_text]
assertEquals("Hello World!", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}
use of com.google.cloud.translate.Translation 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.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateTest method testTranslateText.
@Test
public void testTranslateText() {
Translation translation = TRANSLATE.translate("Hola");
assertEquals("Hello", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}
use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateTest method testTranslateTextWithOptions.
@Test
public void testTranslateTextWithOptions() {
Translation translation = TRANSLATE.translate("Hola", TranslateOption.sourceLanguage("es"), TranslateOption.targetLanguage("de"));
assertEquals("Hallo", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
}
use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateTest method testTranslateTextList.
@Test
public void testTranslateTextList() {
List<Translation> translations = TRANSLATE.translate(ImmutableList.of("Hola", "Hallo"));
Translation translation = translations.get(0);
assertEquals("Hello", translation.getTranslatedText());
assertEquals("es", translation.getSourceLanguage());
translation = translations.get(1);
assertEquals("Hello", translation.getTranslatedText());
assertEquals("de", translation.getSourceLanguage());
}
Aggregations