Search in sources :

Example 6 with Translation

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());
}
Also used : Translation(com.google.cloud.translate.Translation) Test(org.junit.Test)

Example 7 with Translation

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());
}
Also used : Translation(com.google.cloud.translate.Translation) Detection(com.google.cloud.translate.Detection) Translate(com.google.cloud.translate.Translate)

Example 8 with Translation

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());
}
Also used : Translation(com.google.cloud.translate.Translation) Test(org.junit.Test)

Example 9 with Translation

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());
}
Also used : Translation(com.google.cloud.translate.Translation) Test(org.junit.Test)

Example 10 with Translation

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());
}
Also used : Translation(com.google.cloud.translate.Translation) Test(org.junit.Test)

Aggregations

Translation (com.google.cloud.translate.Translation)17 Test (org.junit.Test)9 Translate (com.google.cloud.translate.Translate)7 TranslateOption (com.google.cloud.translate.Translate.TranslateOption)3 LinkedList (java.util.LinkedList)2 PubSubMessage (com.getstarted.background.objects.PubSubMessage)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 Firestore (com.google.cloud.firestore.Firestore)1 WriteResult (com.google.cloud.firestore.WriteResult)1 Detection (com.google.cloud.translate.Detection)1 TranslateException (com.google.cloud.translate.TranslateException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1