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());
}
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());
}
}
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());
}
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);
}
}
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());
}
Aggregations