Search in sources :

Example 11 with Translation

use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.

the class ITTranslateTest method testTranslateTextListWithModel.

@Test
public void testTranslateTextListWithModel() {
    List<Translation> translations = TRANSLATE.translate(ImmutableList.of("Hola", "Hallo"), TranslateOption.model("nmt"));
    Translation translation = translations.get(0);
    assertEquals("Hello", translation.getTranslatedText());
    assertEquals("es", translation.getSourceLanguage());
    assertEquals("nmt", translation.getModel());
    translation = translations.get(1);
    assertEquals("Hello", translation.getTranslatedText());
    assertEquals("de", translation.getSourceLanguage());
    assertEquals("nmt", translation.getModel());
}
Also used : Translation(com.google.cloud.translate.Translation) Test(org.junit.Test)

Example 12 with Translation

use of com.google.cloud.translate.Translation in project java-docs-samples by GoogleCloudPlatform.

the class Translate method translateText.

/**
 * Translate the source text from source to target language.
 *
 * @param sourceText source text to be translated
 * @param sourceLang source language of the text
 * @param targetLang target language of translated text
 * @return source text translated into target language.
 */
public static String translateText(String sourceText, String sourceLang, String targetLang) {
    if (Strings.isNullOrEmpty(sourceLang) || Strings.isNullOrEmpty(targetLang) || sourceLang.equals(targetLang)) {
        return sourceText;
    }
    com.google.cloud.translate.Translate translate = createTranslateService();
    TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
    TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
    Translation translation = translate.translate(sourceText, srcLang, tgtLang);
    return translation.getTranslatedText();
}
Also used : Translation(com.google.cloud.translate.Translation) TranslateOption(com.google.cloud.translate.Translate.TranslateOption)

Example 13 with Translation

use of com.google.cloud.translate.Translation in project java-docs-samples by GoogleCloudPlatform.

the class TranslateText method translateTextWithOptionsAndModel.

/**
 * Translate the source text from source to target language.
 * Make sure that your project is whitelisted.
 *
 * @param sourceText source text to be translated
 * @param sourceLang source language of the text
 * @param targetLang target language of translated text
 * @param out print stream
 */
public static void translateTextWithOptionsAndModel(String sourceText, String sourceLang, String targetLang, PrintStream out) {
    Translate translate = createTranslateService();
    TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
    TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
    // Use translate `model` parameter with `base` and `nmt` options.
    TranslateOption model = TranslateOption.model("nmt");
    Translation translation = translate.translate(sourceText, srcLang, tgtLang, model);
    out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText);
    out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.getTranslatedText());
}
Also used : Translation(com.google.cloud.translate.Translation) TranslateOption(com.google.cloud.translate.Translate.TranslateOption) Translate(com.google.cloud.translate.Translate)

Example 14 with Translation

use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.

the class ITTranslateSnippets method testTranslateTextWithModel.

@Test
public void testTranslateTextWithModel() {
    // [START translate_text_with_model]
    Translation translation = translate.translate("Hola Mundo!", Translate.TranslateOption.sourceLanguage("es"), Translate.TranslateOption.targetLanguage("de"), // Use "base" for standard edition, "nmt" for the premium model.
    Translate.TranslateOption.model("nmt"));
    System.out.printf("TranslatedText:\nText: %s\n", translation.getTranslatedText());
    // [END translate_text_with_model]
    assertEquals("Hallo Welt!", translation.getTranslatedText());
    assertEquals("es", translation.getSourceLanguage());
}
Also used : Translation(com.google.cloud.translate.Translation) Test(org.junit.Test)

Example 15 with Translation

use of com.google.cloud.translate.Translation in project google-cloud-java by GoogleCloudPlatform.

the class ITTranslateSnippets method testTranslateTextListWithOptions.

@Test
public void testTranslateTextListWithOptions() {
    // SNIPPET translateTextsWithOptions
    List<String> texts = new LinkedList<>();
    texts.add("¡Hola Mundo!");
    List<Translation> translations = translate.translate(texts, Translate.TranslateOption.sourceLanguage("es"), Translate.TranslateOption.targetLanguage("de"));
    // SNIPPET translateTextsWithOptions
    Translation translation = translations.get(0);
    assertEquals("Hallo Welt!", translation.getTranslatedText());
    assertEquals("es", translation.getSourceLanguage());
}
Also used : Translation(com.google.cloud.translate.Translation) LinkedList(java.util.LinkedList) 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