Search in sources :

Example 1 with TranslateOption

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

the class TranslateTest method testTranslateOptions.

@Test
public void testTranslateOptions() {
    // target language
    TranslateOption translateOption = TranslateOption.targetLanguage(LANGUAGE);
    assertEquals(TranslateRpc.Option.TARGET_LANGUAGE, translateOption.getRpcOption());
    assertEquals(LANGUAGE, translateOption.getValue());
    // source language
    translateOption = TranslateOption.sourceLanguage(LANGUAGE);
    assertEquals(TranslateRpc.Option.SOURCE_LANGUAGE, translateOption.getRpcOption());
    assertEquals(LANGUAGE, translateOption.getValue());
}
Also used : TranslateOption(com.google.cloud.translate.Translate.TranslateOption) Test(org.junit.Test)

Example 2 with TranslateOption

use of com.google.cloud.translate.Translate.TranslateOption 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 3 with TranslateOption

use of com.google.cloud.translate.Translate.TranslateOption 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 4 with TranslateOption

use of com.google.cloud.translate.Translate.TranslateOption 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)

Aggregations

TranslateOption (com.google.cloud.translate.Translate.TranslateOption)4 Translation (com.google.cloud.translate.Translation)3 Translate (com.google.cloud.translate.Translate)2 Test (org.junit.Test)1