Search in sources :

Example 1 with Translate

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

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

the class TranslateText method displaySupportedLanguages.

/**
 * Displays a list of supported languages and codes.
 *
 * @param out print stream
 * @param tgtLang optional target language
 */
public static void displaySupportedLanguages(PrintStream out, Optional<String> tgtLang) {
    Translate translate = createTranslateService();
    LanguageListOption target = LanguageListOption.targetLanguage(tgtLang.orElse("en"));
    List<Language> languages = translate.listSupportedLanguages(target);
    for (Language language : languages) {
        out.printf("Name: %s, Code: %s\n", language.getName(), language.getCode());
    }
}
Also used : Language(com.google.cloud.translate.Language) LanguageListOption(com.google.cloud.translate.Translate.LanguageListOption) Translate(com.google.cloud.translate.Translate)

Example 3 with Translate

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

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

the class TranslateText method detectLanguage.

/**
 * Detect the language of input text.
 *
 * @param sourceText source text to be detected for language
 * @param out print stream
 */
public static void detectLanguage(String sourceText, PrintStream out) {
    Translate translate = createTranslateService();
    List<Detection> detections = translate.detect(ImmutableList.of(sourceText));
    System.out.println("Language(s) detected:");
    for (Detection detection : detections) {
        out.printf("\t%s\n", detection);
    }
}
Also used : Detection(com.google.cloud.translate.Detection) Translate(com.google.cloud.translate.Translate)

Example 5 with Translate

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

Aggregations

Translate (com.google.cloud.translate.Translate)11 Translation (com.google.cloud.translate.Translation)7 Firestore (com.google.cloud.firestore.Firestore)2 Detection (com.google.cloud.translate.Detection)2 TranslateOption (com.google.cloud.translate.Translate.TranslateOption)2 PubSubMessage (com.getstarted.background.objects.PubSubMessage)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 WriteResult (com.google.cloud.firestore.WriteResult)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1 Language (com.google.cloud.translate.Language)1 LanguageListOption (com.google.cloud.translate.Translate.LanguageListOption)1 TranslateException (com.google.cloud.translate.TranslateException)1 TranslateOptions (com.google.cloud.translate.TranslateOptions)1 TopicName (com.google.pubsub.v1.TopicName)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1