Search in sources :

Example 1 with Translation

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

the class ITTranslateSnippets method testTranslateTextWithOptions.

@Test
public void testTranslateTextWithOptions() {
    Translation translation = translateSnippets.translateTextWithOptions();
    assertEquals("Hallo Welt!", translation.getTranslatedText());
    assertEquals("es", translation.getSourceLanguage());
}
Also used : Translation(com.google.cloud.translate.Translation) Test(org.junit.Test)

Example 2 with Translation

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

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    Translate translate = TranslateOptions.getDefaultInstance().getService();
    // The text to translate
    String text = "Hello, world!";
    // Translates some text into Russian
    Translation translation = translate.translate(text, TranslateOption.sourceLanguage("en"), TranslateOption.targetLanguage("ru"));
    System.out.printf("Text: %s%n", text);
    System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
Also used : Translation(com.google.cloud.translate.Translation) Translate(com.google.cloud.translate.Translate)

Example 3 with Translation

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

the class TranslateText method translateTextWithOptions.

/**
 * 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
 * @param out print stream
 */
public static void translateTextWithOptions(String sourceText, String sourceLang, String targetLang, PrintStream out) {
    Translate translate = createTranslateService();
    TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
    TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
    Translation translation = translate.translate(sourceText, srcLang, tgtLang);
    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 4 with Translation

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

the class TranslateText method translateText.

/**
 * Translates the source text in any language to English.
 *
 * @param sourceText source text to be translated
 * @param out print stream
 */
public static void translateText(String sourceText, PrintStream out) {
    Translate translate = createTranslateService();
    Translation translation = translate.translate(sourceText);
    out.printf("Source Text:\n\t%s\n", sourceText);
    out.printf("Translated Text:\n\t%s\n", translation.getTranslatedText());
}
Also used : Translation(com.google.cloud.translate.Translation) Translate(com.google.cloud.translate.Translate)

Example 5 with Translation

use of com.google.cloud.translate.Translation in project structr by structr.

the class TranslateFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (arrayHasLengthAndAllElementsNotNull(sources, 3)) {
        try {
            final String gctAPIKey = TranslationModule.TranslationAPIKey.getValue();
            if (gctAPIKey == null) {
                logger.error("Google Cloud Translation API Key not configured in structr.conf");
                return "";
            }
            final String text = sources[0].toString();
            final String sourceLanguage = sources[1].toString();
            final String targetLanguage = sources[2].toString();
            final Translate translate = TranslateOptions.builder().apiKey(gctAPIKey).build().service();
            Translation translation = translate.translate(text, TranslateOption.sourceLanguage(sourceLanguage), TranslateOption.targetLanguage(targetLanguage));
            return translation.translatedText();
        } catch (TranslateException te) {
            logger.error("TranslateException: {}", te.getLocalizedMessage());
        } catch (Throwable t) {
            logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) });
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : Translation(com.google.cloud.translate.Translation) TranslateException(com.google.cloud.translate.TranslateException) Translate(com.google.cloud.translate.Translate)

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