1616
1717package com .google .cloud .translate .samples ;
1818
19- import java .util .List ;
20- import java .io .PrintStream ;
21- import com .google .common .collect .ImmutableList ;
22-
2319import com .google .cloud .translate .Detection ;
20+ import com .google .cloud .translate .Language ;
2421import com .google .cloud .translate .Translate ;
2522import com .google .cloud .translate .Translate .TranslateOption ;
26- import com .google .cloud .translate .Language ;
2723import com .google .cloud .translate .Translation ;
2824import com .google .cloud .translate .testing .RemoteTranslateHelper ;
25+ import com .google .common .collect .ImmutableList ;
26+
27+ import java .io .PrintStream ;
28+ import java .util .List ;
2929
3030public class TranslateText {
3131 private static final Translate TRANSLATE = RemoteTranslateHelper .create ().options ().service ();
3232
3333 /**
34- * Detect the language of input text
34+ * Detect the language of input text.
3535 */
3636 public static void detectLanguage (String sourceText , PrintStream out ) {
3737 List <Detection > detections = TRANSLATE .detect (ImmutableList .of (sourceText ));
3838 System .out .println ("Language(s) detected:" );
39- for (Detection detection : detections ) {
39+ for (Detection detection : detections ) {
4040 out .printf ("\t %s\n " , detection );
4141 }
4242 }
4343
4444 /**
45- * Translates the source text in any language to english
45+ * Translates the source text in any language to english.
4646 */
4747 public static void translateText (String sourceText , PrintStream out ) {
4848 Translation translation = TRANSLATE .translate (sourceText );
@@ -51,7 +51,7 @@ public static void translateText(String sourceText, PrintStream out) {
5151 }
5252
5353 /**
54- * Translate the source text from source to target language
54+ * Translate the source text from source to target language.
5555 */
5656 public static void translateTextWithOptions (
5757 String sourceText ,
@@ -73,7 +73,7 @@ public static void translateTextWithOptions(
7373 public static void displaySupportedLanguages (PrintStream out ) {
7474 List <Language > languages = TRANSLATE .listSupportedLanguages ();
7575
76- for (Language language : languages ) {
76+ for (Language language : languages ) {
7777 out .printf ("Name: %s, Code: %s\n " , language .name (), language .code ());
7878 }
7979 }
@@ -82,21 +82,19 @@ public static void main(String[] args) {
8282 String command = args [0 ];
8383 String text ;
8484
85- if (command .equals ("detect" )) {
85+ if (command .equals ("detect" )) {
8686 text = args [1 ];
8787 TranslateText .detectLanguage (text , System .out );
88- }
89- else if (command .equals ("translate" )) {
88+ } else if (command .equals ("translate" )) {
9089 text = args [1 ];
9190 try {
9291 String sourceLang = args [2 ];
9392 String targetLang = args [3 ];
9493 TranslateText .translateTextWithOptions (text , sourceLang , targetLang , System .out );
95- } catch (ArrayIndexOutOfBoundsException ex ) {
94+ } catch (ArrayIndexOutOfBoundsException ex ) {
9695 TranslateText .translateText (text , System .out );
9796 }
98- }
99- else if (command .equals ("langsupport" )) {
97+ } else if (command .equals ("langsupport" )) {
10098 TranslateText .displaySupportedLanguages (System .out );
10199 }
102100 }
0 commit comments