|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.translate.samples; |
| 18 | + |
| 19 | +import com.google.cloud.RetryParams; |
| 20 | +import com.google.cloud.translate.Detection; |
| 21 | +import com.google.cloud.translate.Language; |
| 22 | +import com.google.cloud.translate.Translate; |
| 23 | +import com.google.cloud.translate.Translate.TranslateOption; |
| 24 | +import com.google.cloud.translate.TranslateOptions; |
| 25 | +import com.google.cloud.translate.Translation; |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | + |
| 28 | +import java.io.PrintStream; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +public class TranslateText { |
| 32 | + /** |
| 33 | + * Detect the language of input text. |
| 34 | + * |
| 35 | + * @param sourceText source text to be detected for language |
| 36 | + * @param out print stream |
| 37 | + */ |
| 38 | + public static void detectLanguage(String sourceText, PrintStream out) { |
| 39 | + Translate translate = createTranslateService(); |
| 40 | + List<Detection> detections = translate.detect(ImmutableList.of(sourceText)); |
| 41 | + System.out.println("Language(s) detected:"); |
| 42 | + for (Detection detection : detections) { |
| 43 | + out.printf("\t%s\n", detection); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Translates the source text in any language to english. |
| 49 | + * |
| 50 | + * @param sourceText source text to be translated |
| 51 | + * @param out print stream |
| 52 | + */ |
| 53 | + public static void translateText(String sourceText, PrintStream out) { |
| 54 | + Translate translate = createTranslateService(); |
| 55 | + Translation translation = translate.translate(sourceText); |
| 56 | + out.printf("Source Text:\n\t%s\n", sourceText); |
| 57 | + out.printf("Translated Text:\n\t%s\n", translation.translatedText()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Translate the source text from source to target language. |
| 62 | + * |
| 63 | + * @param sourceText source text to be translated |
| 64 | + * @param sourceLang source language of the text |
| 65 | + * @param targetLang target language of translated text |
| 66 | + * @param out print stream |
| 67 | + */ |
| 68 | + public static void translateTextWithOptions( |
| 69 | + String sourceText, |
| 70 | + String sourceLang, |
| 71 | + String targetLang, |
| 72 | + PrintStream out) { |
| 73 | + |
| 74 | + Translate translate = createTranslateService(); |
| 75 | + TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang); |
| 76 | + TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang); |
| 77 | + |
| 78 | + Translation translation = translate.translate(sourceText, srcLang, tgtLang); |
| 79 | + out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText); |
| 80 | + out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, translation.translatedText()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Displays a list of supported languages and codes. |
| 85 | + * |
| 86 | + * @param out print stream |
| 87 | + */ |
| 88 | + public static void displaySupportedLanguages(PrintStream out) { |
| 89 | + Translate translate = createTranslateService(); |
| 90 | + List<Language> languages = translate.listSupportedLanguages(); |
| 91 | + |
| 92 | + for (Language language : languages) { |
| 93 | + out.printf("Name: %s, Code: %s\n", language.name(), language.code()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Create Google Translate API Service. |
| 99 | + * |
| 100 | + * @return Google Translate Service |
| 101 | + */ |
| 102 | + public static Translate createTranslateService() { |
| 103 | + TranslateOptions translateOption = TranslateOptions.builder() |
| 104 | + .retryParams(retryParams()) |
| 105 | + .connectTimeout(60000) |
| 106 | + .readTimeout(60000) |
| 107 | + .build(); |
| 108 | + return translateOption.service(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Retry params for the Translate API. |
| 113 | + */ |
| 114 | + private static RetryParams retryParams() { |
| 115 | + return RetryParams.builder() |
| 116 | + .retryMaxAttempts(3) |
| 117 | + .maxRetryDelayMillis(30000) |
| 118 | + .totalRetryPeriodMillis(120000) |
| 119 | + .initialRetryDelayMillis(250) |
| 120 | + .build(); |
| 121 | + } |
| 122 | + |
| 123 | + public static void main(String[] args) { |
| 124 | + String command = args[0]; |
| 125 | + String text; |
| 126 | + |
| 127 | + if (command.equals("detect")) { |
| 128 | + text = args[1]; |
| 129 | + TranslateText.detectLanguage(text, System.out); |
| 130 | + } else if (command.equals("translate")) { |
| 131 | + text = args[1]; |
| 132 | + try { |
| 133 | + String sourceLang = args[2]; |
| 134 | + String targetLang = args[3]; |
| 135 | + TranslateText.translateTextWithOptions(text, sourceLang, targetLang, System.out); |
| 136 | + } catch (ArrayIndexOutOfBoundsException ex) { |
| 137 | + TranslateText.translateText(text, System.out); |
| 138 | + } |
| 139 | + } else if (command.equals("langsupport")) { |
| 140 | + TranslateText.displaySupportedLanguages(System.out); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments